-
-
Notifications
You must be signed in to change notification settings - Fork 163
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 #29 from samchon/v2.0
Fix #28
- Loading branch information
Showing
14 changed files
with
253 additions
and
31 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
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
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
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
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import TSON from "../../src"; | ||
|
||
export function test_stringify_to_json_to_array(): void | ||
{ | ||
const something: Something = new Something(10); | ||
const json: string = TSON.stringify<Something>(something); | ||
const expected: string = JSON.stringify(something); | ||
|
||
if (json !== expected) | ||
throw new Error("Bug on TSON.stringify(): failed to undertand the Object.toJSON() returning array."); | ||
} | ||
|
||
class Something | ||
{ | ||
public constructor(public readonly value: number) | ||
{ | ||
} | ||
|
||
public toJSON(): number[] | ||
{ | ||
return new Array(this.value) | ||
.fill(0) | ||
.map((_, index) => index + 1); | ||
} | ||
} |
4 changes: 2 additions & 2 deletions
4
.../features/test_stringify_to_json_class.ts → ...tures/test_stringify_to_json_to_atomic.ts
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
31 changes: 31 additions & 0 deletions
31
test/features/test_stringify_to_json_to_atomic_nullable.ts
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 |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import TSON from "../../src"; | ||
|
||
export function test_stringify_to_json_to_atomic_nullable(): void | ||
{ | ||
for (const value of [1, 2]) | ||
test(value); | ||
} | ||
|
||
function test(value: number): void | ||
{ | ||
const something: Something = new Something(value); | ||
const json: string = TSON.stringify<Something>(something); | ||
const expected: string = JSON.stringify(something); | ||
|
||
if (json !== expected) | ||
throw new Error("Bug on TSON.stringify(): failed to undertand the Object.toJSON() returning nullable atomic value."); | ||
} | ||
|
||
class Something | ||
{ | ||
public constructor(public readonly value: number) | ||
{ | ||
} | ||
|
||
public toJSON(): null | number | ||
{ | ||
return this.value % 2 === 0 | ||
? this.value | ||
: null; | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import TSON from "../../src"; | ||
|
||
export function test_stringify_to_json_to_atomic_union(): void | ||
{ | ||
for (const value of [1, 2]) | ||
test(value); | ||
|
||
} | ||
|
||
function test(value: number): void | ||
{ | ||
const something: Something = new Something(value); | ||
const json: string = TSON.stringify<Something>(something); | ||
const expected: string = JSON.stringify(something); | ||
|
||
if (json !== expected) | ||
throw new Error("Bug on TSON.stringify(): failed to undertand the Object.toJSON() returning atomic union value."); | ||
} | ||
|
||
class Something | ||
{ | ||
public constructor(public readonly value: number) | ||
{ | ||
} | ||
|
||
public toJSON(): number | string | ||
{ | ||
return this.value % 2 === 0 | ||
? this.value | ||
: this.value.toString(); | ||
} | ||
} |
7 changes: 4 additions & 3 deletions
7
...s/test_stringify_to_json_class_closure.ts → ...est_stringify_to_json_to_class_closure.ts
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
49 changes: 49 additions & 0 deletions
49
test/features/test_stringify_to_json_to_complicate_union.ts
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 |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import TSON from "../../src"; | ||
|
||
export function test_stringify_to_json_to_complicated_union(): void | ||
{ | ||
new Array(5) | ||
.fill(0) | ||
.forEach((_, index) => test(index + 1)); | ||
} | ||
|
||
function test(value: number): void | ||
{ | ||
const something: Something = new Something(value); | ||
const json: string = TSON.stringify(something); | ||
const expected: string = JSON.stringify(something); | ||
|
||
if (json !== expected) | ||
throw new Error("Bug on TSON.stringify(): failed to understand the Object.toJSON() returning complicate union."); | ||
} | ||
|
||
class Something | ||
{ | ||
public constructor(public readonly value: number) | ||
{ | ||
} | ||
|
||
public toJSON(): IX | IY | number[] | string | null | ||
{ | ||
if (this.value === 1) | ||
return { x: 1 }; | ||
else if (this.value === 2) | ||
return { y: "2" }; | ||
else if (this.value === 3) | ||
return [1, 2, 3]; | ||
else if (this.value === 4) | ||
return "4"; | ||
else | ||
return null; | ||
} | ||
} | ||
|
||
interface IX | ||
{ | ||
x: number; | ||
} | ||
|
||
interface IY | ||
{ | ||
y: string; | ||
} |
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 |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import TSON from "../../src"; | ||
|
||
export function test_stringify_to_json_to_object(): void | ||
{ | ||
const operator: Operator = new Operator(2, 3, 4); | ||
const json: string = TSON.stringify<Operator>(operator); | ||
const expected: string = JSON.stringify(operator); | ||
|
||
if (json !== expected) | ||
throw new Error("Bug on TSON.stringify(): failed to understand the Object.toJSON() returning object."); | ||
} | ||
|
||
class Operator | ||
{ | ||
public constructor | ||
( | ||
public readonly x: number, | ||
public readonly y: number, | ||
public readonly z: number | ||
) | ||
{ | ||
} | ||
|
||
public toJSON() | ||
{ | ||
return { | ||
x: this.x, | ||
y: this.y, | ||
z: this.z, | ||
solution: this.x + this.y + this.z | ||
}; | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import TSON from "../../src"; | ||
|
||
export function test_stringify_to_json_to_object_closure(): void | ||
{ | ||
const obj = { | ||
value: 9, | ||
toJSON: () => 2 + 3 + 4 | ||
}; | ||
const json: string = TSON.stringify<typeof obj>(obj); | ||
const expected: string = JSON.stringify(obj); | ||
|
||
if (json !== expected) | ||
throw new Error("Bug on TSON.stringify(): failed to understand the primitive toJSON() closure function."); | ||
} |
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 |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import TSON from "../../src"; | ||
|
||
export function test_stringify_to_json_to_object_union(): void | ||
{ | ||
for (const value of [1, 2]) | ||
test(value); | ||
} | ||
|
||
function test(value: number): void | ||
{ | ||
const something: Something = new Something(value); | ||
const json: string = TSON.stringify<Something>(something); | ||
const expected: string = JSON.stringify(something); | ||
|
||
if (json !== expected) | ||
throw new Error("Bug on TSON.stringify(): failed to undertand the Object.toJSON() returning union object."); | ||
} | ||
|
||
class Something | ||
{ | ||
public constructor(public readonly value: number) | ||
{ | ||
} | ||
|
||
public toJSON(): IX | IY | ||
{ | ||
return this.value % 2 === 0 | ||
? { x: this.value } | ||
: { y: this.value.toString() }; | ||
} | ||
} | ||
|
||
interface IX | ||
{ | ||
x: number; | ||
} | ||
|
||
interface IY | ||
{ | ||
y: string; | ||
} |