Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nested实现 #56

Open
i5ting opened this issue Oct 26, 2022 · 10 comments
Open

nested实现 #56

i5ting opened this issue Oct 26, 2022 · 10 comments

Comments

@i5ting
Copy link
Owner

i5ting commented Oct 26, 2022

https://junit.org/junit5/docs/current/user-guide/#writing-tests-nested

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.util.EmptyStackException;
import java.util.Stack;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;

@DisplayName("A stack")
class TestingAStackDemo {

    Stack<Object> stack;

    @Test
    @DisplayName("is instantiated with new Stack()")
    void isInstantiatedWithNew() {
        new Stack<>();
    }

    @Nested
    @DisplayName("when new")
    class WhenNew {

        @BeforeEach
        void createNewStack() {
            stack = new Stack<>();
        }

        @Test
        @DisplayName("is empty")
        void isEmpty() {
            assertTrue(stack.isEmpty());
        }

        @Test
        @DisplayName("throws EmptyStackException when popped")
        void throwsExceptionWhenPopped() {
            assertThrows(EmptyStackException.class, stack::pop);
        }

        @Test
        @DisplayName("throws EmptyStackException when peeked")
        void throwsExceptionWhenPeeked() {
            assertThrows(EmptyStackException.class, stack::peek);
        }

        @Nested
        @DisplayName("after pushing an element")
        class AfterPushing {

            String anElement = "an element";

            @BeforeEach
            void pushAnElement() {
                stack.push(anElement);
            }

            @Test
            @DisplayName("it is no longer empty")
            void isNotEmpty() {
                assertFalse(stack.isEmpty());
            }

            @Test
            @DisplayName("returns the element when popped and is empty")
            void returnElementWhenPopped() {
                assertEquals(anElement, stack.pop());
                assertTrue(stack.isEmpty());
            }

            @Test
            @DisplayName("returns the element when peeked but remains not empty")
            void returnElementWhenPeeked() {
                assertEquals(anElement, stack.peek());
                assertFalse(stack.isEmpty());
            }
        }
    }
}
@i5ting
Copy link
Owner Author

i5ting commented Oct 26, 2022

// @ts-nocheck
import * as assert from "uvu/assert";

import { Test, BeforeEach, DisplayName, Nested } from "@ts-junit/core";

@DisplayName("A stack")
export default class TestingAStackDemo {
  stack: object[];

  @Test
  @DisplayName("is instantiated with new Stack()")
  isInstantiatedWithNew(): void {
    console.dir("isInstantiatedWithNew");
    this.stack = new Array();
  }

  @Nested
  @DisplayName("when new")
  a = class WhenNew {
    @BeforeEach
    createNewStack(): void {
      stack = new Stack<>();
    }

    @Test
    @DisplayName("is empty")
    isEmpty(): void {
      assertTrue(stack.isEmpty());
    }

    @Test
    @DisplayName("throws EmptyStackException when popped")
    throwsExceptionWhenPopped(): void {
      assertThrows(EmptyStackException.class, stack);
    }

    @Test
    @DisplayName("throws EmptyStackException when peeked")
    throwsExceptionWhenPeeked(): void {
      assertThrows(EmptyStackException.class, stack);
    }

    @Nested
    @DisplayName("after pushing an element")
    b = class AfterPushing {
      anElement: String = "an element";

      @BeforeEach
      pushAnElement(): void {
        stack.push(anElement);
      }

      @Test
      @DisplayName("it is no longer empty")
      isNotEmpty(): void {
        assertFalse(stack.isEmpty());
      }

      @Test
      @DisplayName("returns the element when popped and is empty")
      returnElementWhenPopped(): void {
        assertEquals(anElement, stack.pop());
        assertTrue(stack.isEmpty());
      }

      @Test
      @DisplayName("returns the element when peeked but remains not empty")
      returnElementWhenPeeked(): void {
        assertEquals(anElement, stack.peek());
        assertFalse(stack.isEmpty());
      }
    };
  };
}

@i5ting
Copy link
Owner Author

i5ting commented Oct 26, 2022

"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
exports.__esModule = true;
var core_1 = require("@ts-junit/core");
var TestingAStackDemo = /** @class */ (function () {
    function TestingAStackDemo() {
        this.a = /** @class */ (function () {
            function WhenNew() {
                this.b = /** @class */ (function () {
                    function AfterPushing() {
                        this.anElement = "an element";
                    }
                    AfterPushing.prototype.pushAnElement = function () {
                        stack.push(anElement);
                    };
                    AfterPushing.prototype.isNotEmpty = function () {
                        assertFalse(stack.isEmpty());
                    };
                    AfterPushing.prototype.returnElementWhenPopped = function () {
                        assertEquals(anElement, stack.pop());
                        assertTrue(stack.isEmpty());
                    };
                    AfterPushing.prototype.returnElementWhenPeeked = function () {
                        assertEquals(anElement, stack.peek());
                        assertFalse(stack.isEmpty());
                    };
                    return AfterPushing;
                }());
            }
            WhenNew.prototype.createNewStack = function () {
                stack = new Stack();
            };
            WhenNew.prototype.isEmpty = function () {
                assertTrue(stack.isEmpty());
            };
            WhenNew.prototype.throwsExceptionWhenPopped = function () {
                assertThrows(EmptyStackException["class"], stack);
            };
            WhenNew.prototype.throwsExceptionWhenPeeked = function () {
                assertThrows(EmptyStackException["class"], stack);
            };
            return WhenNew;
        }());
    }
    TestingAStackDemo.prototype.isInstantiatedWithNew = function () {
        console.dir("isInstantiatedWithNew");
        this.stack = new Array();
    };
    __decorate([
        core_1.Test,
        (0, core_1.DisplayName)("is instantiated with new Stack()")
    ], TestingAStackDemo.prototype, "isInstantiatedWithNew");
    __decorate([
        core_1.Nested,
        (0, core_1.DisplayName)("when new")
    ], TestingAStackDemo.prototype, "a");
    TestingAStackDemo = __decorate([
        (0, core_1.DisplayName)("A stack")
    ], TestingAStackDemo);
    return TestingAStackDemo;
}());
exports["default"] = TestingAStackDemo;

@justjavac
Copy link
Collaborator

一个思路:see playground

// @ts-nocheck
import * as assert from "uvu/assert";

import { Test, BeforeEach, DisplayName, Nested } from "@ts-junit/core";

export default class TestingAStackDemo {
  stack: object[];

  isInstantiatedWithNew(): void {
    console.dir("isInstantiatedWithNew");
    this.stack = new Array();
  }

  a() {
    @Nested
    class Foo {
      @BeforeEach
      createNewStack(): void {
        stack = new Stack<>();
      }

      @Test
      @DisplayName("[nested]: is empty")
      isEmpty(): void {
        assertTrue(stack.isEmpty());
      }

      @Test
      @DisplayName("[nested]: throws EmptyStackException when popped")
      throwsExceptionWhenPopped(): void {
        assertThrows(EmptyStackException.class, stack);
      }

      @Test
      @DisplayName("[nested]: throws EmptyStackException when peeked")
      throwsExceptionWhenPeeked(): void {
        assertThrows(EmptyStackException.class, stack);
      }
    };
  }
}

@i5ting
Copy link
Owner Author

i5ting commented Oct 26, 2022

@justjavac 妙啊

@i5ting
Copy link
Owner Author

i5ting commented Oct 27, 2022

另一种玩法

  @Nested
  @DisplayName("when new")
  a() {
    class WhenNew {
      @BeforeEach
      createNewStack(): void {
        stack = new Stack<>();
      }

@i5ting
Copy link
Owner Author

i5ting commented Oct 27, 2022

其他方法是类new的时候获取配置信息,而nested方法需要调用一下。if nested method, this.propertyName()

@byteHulk
Copy link
Contributor

// @ts-nocheck
import * as assert from "uvu/assert";

import { Test, BeforeEach, DisplayName, Nested } from "@ts-junit/core";

@DisplayName("A stack")
export default class TestingAStackDemo {
  stack: object[];

  @Test
  @DisplayName("is instantiated with new Stack()")
  isInstantiatedWithNew(): void {
    console.dir("isInstantiatedWithNew");
    this.stack = new Array();
  }

  @Nested
  @DisplayName("when new")
  a = class WhenNew {
    @BeforeEach
    createNewStack(): void {
      stack = new Stack<>();
    }

    @Test
    @DisplayName("is empty")
    isEmpty(): void {
      assertTrue(stack.isEmpty());
    }

    @Test
    @DisplayName("throws EmptyStackException when popped")
    throwsExceptionWhenPopped(): void {
      assertThrows(EmptyStackException.class, stack);
    }

    @Test
    @DisplayName("throws EmptyStackException when peeked")
    throwsExceptionWhenPeeked(): void {
      assertThrows(EmptyStackException.class, stack);
    }

    @Nested
    @DisplayName("after pushing an element")
    b = class AfterPushing {
      anElement: String = "an element";

      @BeforeEach
      pushAnElement(): void {
        stack.push(anElement);
      }

      @Test
      @DisplayName("it is no longer empty")
      isNotEmpty(): void {
        assertFalse(stack.isEmpty());
      }

      @Test
      @DisplayName("returns the element when popped and is empty")
      returnElementWhenPopped(): void {
        assertEquals(anElement, stack.pop());
        assertTrue(stack.isEmpty());
      }

      @Test
      @DisplayName("returns the element when peeked but remains not empty")
      returnElementWhenPeeked(): void {
        assertEquals(anElement, stack.peek());
        assertFalse(stack.isEmpty());
      }
    };
  };
}

nested cache 这样可行吗

{
  clz_name: 'TestingAStackDemo',
  newClz: {
    hook: {},
    cases: {
      isInstantiatedWithNew: {
        desc: 'is instantiated with new Stack()',
        fn: [Function(anonymous)],
      },
    },
    __obj: TestingAStackDemo { stack: [] }
    clz_name: 'WhenNew',
    newClz: {
      hook: {
        'before.each': [Function(anonymous)],
      },
      cases: {
        isEmpty: { desc: 'is empty', fn: [Function(anonymous)] },
        throwsExceptionWhenPopped: {
          desc: 'throws EmptyStackException when popped',
          fn: [Function(anonymous)],
        },
        throwsExceptionWhenPeeked: {
          desc: 'throws EmptyStackException when peeked',
          fn: [Function(anonymous)],
        },
      },
      __obj: WhenNew {}
      clz_name: 'AfterPushing',
      newClz: {
        hook: {
          'before.each': [Function(anonymous)],
        },
        cases: {
          isNotEmpty: {
            desc: 'it is no longer empty',
            fn: [Function(anonymous)],
          },
          returnElementWhenPopped: {
            desc: 'returns the element when popped and is empty',
            fn: [Function(anonymous)],
          },
          returnElementWhenPeeked: {
            desc: 'returns the element when peeked but remains not empty"',
            fn: [Function(anonymous)],
          },
        },
        __obj: AfterPushing {}
      },
    }
  }
}

@justjavac
Copy link
Collaborator

justjavac commented Oct 29, 2022

@byteHulk

@Nested
@DisplayName("when new")
a = class WhenNew {

这样不行。

@i5ting
Copy link
Owner Author

i5ting commented Oct 29, 2022

// @ts-nocheck
import * as assert from "uvu/assert";

import { Test, BeforeEach, DisplayName, Nested } from "@ts-junit/core";

@DisplayName("A stack")
export default class TestingAStackDemo {
  stack: object[];

  @Test
  @DisplayName("is instantiated with new Stack()")
  isInstantiatedWithNew(): void {
    console.dir("isInstantiatedWithNew");
    this.stack = new Array();
  }

  @Nested
  @DisplayName("when new")
  a = class WhenNew {
    @BeforeEach
    createNewStack(): void {
      stack = new Stack<>();
    }

    @Test
    @DisplayName("is empty")
    isEmpty(): void {
      assertTrue(stack.isEmpty());
    }

    @Test
    @DisplayName("throws EmptyStackException when popped")
    throwsExceptionWhenPopped(): void {
      assertThrows(EmptyStackException.class, stack);
    }

    @Test
    @DisplayName("throws EmptyStackException when peeked")
    throwsExceptionWhenPeeked(): void {
      assertThrows(EmptyStackException.class, stack);
    }

    @Nested
    @DisplayName("after pushing an element")
    b = class AfterPushing {
      anElement: String = "an element";

      @BeforeEach
      pushAnElement(): void {
        stack.push(anElement);
      }

      @Test
      @DisplayName("it is no longer empty")
      isNotEmpty(): void {
        assertFalse(stack.isEmpty());
      }

      @Test
      @DisplayName("returns the element when popped and is empty")
      returnElementWhenPopped(): void {
        assertEquals(anElement, stack.pop());
        assertTrue(stack.isEmpty());
      }

      @Test
      @DisplayName("returns the element when peeked but remains not empty")
      returnElementWhenPeeked(): void {
        assertEquals(anElement, stack.peek());
        assertFalse(stack.isEmpty());
      }
    };
  };
}

nested cache 这样可行吗

{
  clz_name: 'TestingAStackDemo',
  newClz: {
    hook: {},
    cases: {
      isInstantiatedWithNew: {
        desc: 'is instantiated with new Stack()',
        fn: [Function(anonymous)],
      },
    },
    __obj: TestingAStackDemo { stack: [] }
    clz_name: 'WhenNew',
    newClz: {
      hook: {
        'before.each': [Function(anonymous)],
      },
      cases: {
        isEmpty: { desc: 'is empty', fn: [Function(anonymous)] },
        throwsExceptionWhenPopped: {
          desc: 'throws EmptyStackException when popped',
          fn: [Function(anonymous)],
        },
        throwsExceptionWhenPeeked: {
          desc: 'throws EmptyStackException when peeked',
          fn: [Function(anonymous)],
        },
      },
      __obj: WhenNew {}
      clz_name: 'AfterPushing',
      newClz: {
        hook: {
          'before.each': [Function(anonymous)],
        },
        cases: {
          isNotEmpty: {
            desc: 'it is no longer empty',
            fn: [Function(anonymous)],
          },
          returnElementWhenPopped: {
            desc: 'returns the element when popped and is empty',
            fn: [Function(anonymous)],
          },
          returnElementWhenPeeked: {
            desc: 'returns the element when peeked but remains not empty"',
            fn: [Function(anonymous)],
          },
        },
        __obj: AfterPushing {}
      },
    }
  }
}

目前写法是这样的,我和jjc确定可行的方案,我这边在实现呢。

// @ts-nocheck
import * as assert from "uvu/assert";

import { Test, BeforeEach, DisplayName, Nested } from "@ts-junit/core";

@DisplayName("A stack")
export default class TestingAStackDemo {
  stack: object[];

  @Test
  @DisplayName("is instantiated with new Stack()")
  isInstantiatedWithNew(): void {
    console.dir("isInstantiatedWithNew");
    this.stack = new Array();
  }

  @Nested
  @DisplayName("when new")
  a() {
    class WhenNew {
      @BeforeEach
      createNewStack(): void {
        stack = new Stack<>();
      }

      @Test
      @DisplayName("is empty")
      isEmpty(): void {
        assertTrue(stack.isEmpty());
      }

      @Test
      @DisplayName("throws EmptyStackException when popped")
      throwsExceptionWhenPopped(): void {
        assertThrows(EmptyStackException.class, stack);
      }

      @Test
      @DisplayName("throws EmptyStackException when peeked")
      throwsExceptionWhenPeeked(): void {
        assertThrows(EmptyStackException.class, stack);
      }

      @Nested
      @DisplayName("after pushing an element")
      b() {
        class AfterPushing {
          anElement: String = "an element";

          @BeforeEach
          pushAnElement(): void {
            stack.push(anElement);
          }

          @Test
          @DisplayName("it is no longer empty")
          isNotEmpty(): void {
            assertFalse(stack.isEmpty());
          }

          @Test
          @DisplayName("returns the element when popped and is empty")
          returnElementWhenPopped(): void {
            assertEquals(anElement, stack.pop());
            assertTrue(stack.isEmpty());
          }

          @Test
          @DisplayName("returns the element when peeked but remains not empty")
          returnElementWhenPeeked(): void {
            assertEquals(anElement, stack.peek());
            assertFalse(stack.isEmpty());
          }
        }
      }
    }
  }
}

@i5ting
Copy link
Owner Author

i5ting commented Oct 29, 2022

1)a 里定义class,但不能return

2)b是a里的class,也就是nested可以无限嵌套。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants