-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #121 from gg-blog/312551063
[LAB2] 312551063
- Loading branch information
Showing
1 changed file
with
85 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,89 @@ | ||
const test = require('node:test'); | ||
const assert = require('assert'); | ||
const { Application, MailSystem } = require('./main'); | ||
const fs = require('fs'); | ||
|
||
|
||
test("Test MailSystem's write", () => { | ||
|
||
const mail=new MailSystem(); | ||
assert.strictEqual(mail.write("Daniel"),'Congrats, Daniel!'); | ||
|
||
}); | ||
|
||
test("Test MailSystem's send", () => { | ||
|
||
const mail=new MailSystem(); | ||
const name="Daniel"; | ||
const context=""; | ||
assert.strictEqual(typeof(mail.send(name,context)), "boolean" ); | ||
assert.strictEqual(typeof(mail.send(name,context)), "boolean" ); | ||
assert.strictEqual(typeof(mail.send(name,context)), "boolean" ); | ||
assert.strictEqual(typeof(mail.send(name,context)), "boolean" ); | ||
assert.strictEqual(typeof(mail.send(name,context)), "boolean" ); | ||
assert.strictEqual(typeof(mail.send(name,context)), "boolean" ); | ||
assert.strictEqual(typeof(mail.send(name,context)), "boolean" ); | ||
}); | ||
|
||
class FakeMailSystem { | ||
constructor() { | ||
this.sended = []; | ||
} | ||
|
||
write(name) { | ||
const context = 'haha,' + name; | ||
console.log("sending"); | ||
return context; | ||
} | ||
|
||
send(name, context) { | ||
this.sended.push(context); | ||
} | ||
} | ||
|
||
test("Test Application's getNames",async () => { | ||
fs.writeFileSync('name_list.txt', 'Daniel\nAndy'); | ||
const app = new Application(); | ||
const result=await app.getNames(); // wait for reading file | ||
|
||
assert.strictEqual(result[0][0],"Daniel"); | ||
assert.strictEqual(result[0][1],"Andy"); | ||
|
||
}); | ||
|
||
test("Test Application's getRandomPerson", async () => { | ||
fs.writeFileSync('name_list.txt', 'Daniel\nAndy'); | ||
const app = new Application(); | ||
const result=await app.getNames(); // wait for reading file | ||
assert.match(app.getRandomPerson(),new RegExp("Daniel|Andy")); | ||
assert.match(app.getRandomPerson(),new RegExp("Daniel|Andy")); | ||
assert.match(app.getRandomPerson(),new RegExp("Daniel|Andy")); | ||
}); | ||
|
||
test("Test Application's selectNextPerson", async () => { | ||
fs.writeFileSync('name_list.txt', 'Daniel\nAndy'); | ||
const app = new Application(); | ||
const result=await app.getNames(); // wait for reading file | ||
assert.match(app.selectNextPerson(),new RegExp("Daniel|Andy")); | ||
assert.notEqual(app.selected,null); | ||
assert.match(app.selectNextPerson(),new RegExp("Daniel|Andy")); | ||
assert.strictEqual(app.selectNextPerson(),null); | ||
|
||
}); | ||
|
||
test("Test Application's notifySelected",async () => { | ||
fs.writeFileSync('name_list.txt', 'Daniel'); | ||
const app = new Application(); | ||
const result=await app.getNames(); // wait for reading file | ||
app.selectNextPerson(); | ||
app.selectNextPerson(); | ||
const faking=new FakeMailSystem(); | ||
app.mailSystem=faking; | ||
app.notifySelected(); | ||
assert.strictEqual(faking.sended[0],"haha,Daniel"); | ||
|
||
|
||
}); | ||
|
||
|
||
|
||
// TODO: write your tests here | ||
// Remember to use Stub, Mock, and Spy when necessary |