-
Notifications
You must be signed in to change notification settings - Fork 7
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
Comments
// @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());
}
};
};
} |
"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; |
一个思路: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);
}
};
}
} |
@justjavac 妙啊 |
|
其他方法是类new的时候获取配置信息,而nested方法需要调用一下。if nested method, this.propertyName() |
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 {}
},
}
}
} |
@Nested
@DisplayName("when new")
a = class WhenNew { 这样不行。 |
目前写法是这样的,我和jjc确定可行的方案,我这边在实现呢。
|
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
https://junit.org/junit5/docs/current/user-guide/#writing-tests-nested
The text was updated successfully, but these errors were encountered: