forked from javiercbk/json_to_dart
-
Notifications
You must be signed in to change notification settings - Fork 0
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 javiercbk#12 from javiercbk/dart2
added test for bug javiercbk#10
- Loading branch information
Showing
3 changed files
with
267 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,22 @@ | ||
{ | ||
"glossary": { | ||
"title": "example glossary", | ||
"GlossDiv": { | ||
"title": "S", | ||
"GlossList": { | ||
"GlossEntry": { | ||
"ID": "SGML", | ||
"SortAs": "SGML", | ||
"GlossTerm": "Standard Generalized Markup Language", | ||
"Acronym": "SGML", | ||
"Abbrev": "ISO 8879:1986", | ||
"GlossDef": { | ||
"para": "A meta-markup language, used to create markup languages such as DocBook.", | ||
"GlossSeeAlso": ["GML", "XML"] | ||
}, | ||
"GlossSee": "markup" | ||
} | ||
} | ||
} | ||
} | ||
} |
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,96 @@ | ||
import 'dart:io'; | ||
import 'dart:convert'; | ||
import "package:path/path.dart" show dirname, join, normalize; | ||
import 'package:test/test.dart'; | ||
import './generated/bug_ten.dart'; | ||
|
||
String _scriptPath() { | ||
var script = Platform.script.toString(); | ||
if (script.startsWith("file://")) { | ||
script = script.substring(7); | ||
} else { | ||
final idx = script.indexOf("file:/"); | ||
script = script.substring(idx + 5); | ||
} | ||
return script; | ||
} | ||
|
||
void main() { | ||
group("model-generator", () { | ||
final currentDirectory = dirname(_scriptPath()); | ||
test("Generated class should correctly parse JSON for bug 10", () { | ||
final jsonPath = normalize(join(currentDirectory, 'bug_10.json')); | ||
final jsonRawData = new File(jsonPath).readAsStringSync(); | ||
Map sampleMap = json.decode(jsonRawData); | ||
final bugTen = new BugTen.fromJson(sampleMap); | ||
expect(bugTen, isNot(isNull)); | ||
expect(bugTen.glossary, isNot(isNull)); | ||
expect(bugTen.glossary.title, equals('example glossary')); | ||
expect(bugTen.glossary.glossDiv, isNot(isNull)); | ||
expect(bugTen.glossary.glossDiv.title, equals("S")); | ||
expect(bugTen.glossary.glossDiv.glossList, isNot(isNull)); | ||
final ge = bugTen.glossary.glossDiv.glossList.glossEntry; | ||
expect(ge, isNot(isNull)); | ||
expect(ge.iD, equals("SGML")); | ||
expect(ge.sortAs, equals("SGML")); | ||
expect(ge.glossTerm, equals("Standard Generalized Markup Language")); | ||
expect(ge.acronym, equals("SGML")); | ||
expect(ge.abbrev, equals("ISO 8879:1986")); | ||
expect(ge.glossSee, equals("markup")); | ||
expect(ge.glossDef, isNot(isNull)); | ||
expect(ge.glossDef.para, equals("A meta-markup language, used to create markup languages such as DocBook.")); | ||
final seeAlso = ge.glossDef.glossSeeAlso; | ||
expect(seeAlso, isNot(isNull)); | ||
expect(seeAlso.length, equals(2)); | ||
expect(seeAlso[0], equals("GML")); | ||
expect(seeAlso[1], equals("XML")); | ||
}); | ||
|
||
test("Generated class should correctly generate JSON", () { | ||
final glossSeeAlso = new List<String>(); | ||
glossSeeAlso.add("GML"); | ||
glossSeeAlso.add("XML"); | ||
final glossDef = new GlossDef( | ||
para: "A meta-markup language, used to create markup languages such as DocBook.", | ||
glossSeeAlso: glossSeeAlso | ||
); | ||
final glossEntry = new GlossEntry( | ||
abbrev: "ISO 8879:1986", | ||
acronym: "SGML", | ||
glossDef: glossDef, | ||
glossSee: "markup", | ||
glossTerm: "Standard Generalized Markup Language", | ||
iD: "SGML", | ||
sortAs: "SGML", | ||
); | ||
final glossList = new GlossList( | ||
glossEntry: glossEntry, | ||
); | ||
final glossDiv = new GlossDiv( | ||
glossList: glossList, | ||
title: "S", | ||
); | ||
final glossary = new Glossary( | ||
glossDiv: glossDiv, | ||
title: "example glossary", | ||
); | ||
final bugTen = new BugTen( | ||
glossary: glossary | ||
); | ||
final codec = new JsonCodec(toEncodable: (dynamic v) => v.toString()); | ||
final encodedJSON = codec.encode(bugTen.toJson()); | ||
print(encodedJSON); | ||
expect(encodedJSON.contains('"title":"example glossary"'), equals(true)); | ||
expect(encodedJSON.contains('"GlossDiv":{"title":"S"'), equals(true)); | ||
expect(encodedJSON.contains('"GlossList":{"GlossEntry":{'), equals(true)); | ||
expect(encodedJSON.contains('"ID":"SGML",'), equals(true)); | ||
expect(encodedJSON.contains('"SortAs":"SGML",'), equals(true)); | ||
expect(encodedJSON.contains('"GlossTerm":"Standard Generalized Markup Language",'), equals(true)); | ||
expect(encodedJSON.contains('"Acronym":"SGML",'), equals(true)); | ||
expect(encodedJSON.contains('"Abbrev":"ISO 8879:1986",'), equals(true)); | ||
expect(encodedJSON.contains('"GlossDef":{"para":"A meta-markup language, used to create markup languages such as DocBook.",'), equals(true)); | ||
expect(encodedJSON.contains('"GlossSeeAlso":["GML","XML"]'), equals(true)); | ||
expect(encodedJSON.contains('"GlossSee":"markup"'), equals(true)); | ||
}); | ||
}); | ||
} |
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,149 @@ | ||
class BugTen { | ||
Glossary glossary; | ||
|
||
BugTen({this.glossary}); | ||
|
||
BugTen.fromJson(Map<String, dynamic> json) { | ||
glossary = json['glossary'] != null | ||
? new Glossary.fromJson(json['glossary']) | ||
: null; | ||
} | ||
|
||
Map<String, dynamic> toJson() { | ||
final Map<String, dynamic> data = new Map<String, dynamic>(); | ||
if (this.glossary != null) { | ||
data['glossary'] = this.glossary.toJson(); | ||
} | ||
return data; | ||
} | ||
} | ||
|
||
class Glossary { | ||
String title; | ||
GlossDiv glossDiv; | ||
|
||
Glossary({this.title, this.glossDiv}); | ||
|
||
Glossary.fromJson(Map<String, dynamic> json) { | ||
title = json['title']; | ||
glossDiv = json['GlossDiv'] != null | ||
? new GlossDiv.fromJson(json['GlossDiv']) | ||
: null; | ||
} | ||
|
||
Map<String, dynamic> toJson() { | ||
final Map<String, dynamic> data = new Map<String, dynamic>(); | ||
data['title'] = this.title; | ||
if (this.glossDiv != null) { | ||
data['GlossDiv'] = this.glossDiv.toJson(); | ||
} | ||
return data; | ||
} | ||
} | ||
|
||
class GlossDiv { | ||
String title; | ||
GlossList glossList; | ||
|
||
GlossDiv({this.title, this.glossList}); | ||
|
||
GlossDiv.fromJson(Map<String, dynamic> json) { | ||
title = json['title']; | ||
glossList = json['GlossList'] != null | ||
? new GlossList.fromJson(json['GlossList']) | ||
: null; | ||
} | ||
|
||
Map<String, dynamic> toJson() { | ||
final Map<String, dynamic> data = new Map<String, dynamic>(); | ||
data['title'] = this.title; | ||
if (this.glossList != null) { | ||
data['GlossList'] = this.glossList.toJson(); | ||
} | ||
return data; | ||
} | ||
} | ||
|
||
class GlossList { | ||
GlossEntry glossEntry; | ||
|
||
GlossList({this.glossEntry}); | ||
|
||
GlossList.fromJson(Map<String, dynamic> json) { | ||
glossEntry = json['GlossEntry'] != null | ||
? new GlossEntry.fromJson(json['GlossEntry']) | ||
: null; | ||
} | ||
|
||
Map<String, dynamic> toJson() { | ||
final Map<String, dynamic> data = new Map<String, dynamic>(); | ||
if (this.glossEntry != null) { | ||
data['GlossEntry'] = this.glossEntry.toJson(); | ||
} | ||
return data; | ||
} | ||
} | ||
|
||
class GlossEntry { | ||
String iD; | ||
String sortAs; | ||
String glossTerm; | ||
String acronym; | ||
String abbrev; | ||
GlossDef glossDef; | ||
String glossSee; | ||
|
||
GlossEntry( | ||
{this.iD, | ||
this.sortAs, | ||
this.glossTerm, | ||
this.acronym, | ||
this.abbrev, | ||
this.glossDef, | ||
this.glossSee}); | ||
|
||
GlossEntry.fromJson(Map<String, dynamic> json) { | ||
iD = json['ID']; | ||
sortAs = json['SortAs']; | ||
glossTerm = json['GlossTerm']; | ||
acronym = json['Acronym']; | ||
abbrev = json['Abbrev']; | ||
glossDef = json['GlossDef'] != null | ||
? new GlossDef.fromJson(json['GlossDef']) | ||
: null; | ||
glossSee = json['GlossSee']; | ||
} | ||
|
||
Map<String, dynamic> toJson() { | ||
final Map<String, dynamic> data = new Map<String, dynamic>(); | ||
data['ID'] = this.iD; | ||
data['SortAs'] = this.sortAs; | ||
data['GlossTerm'] = this.glossTerm; | ||
data['Acronym'] = this.acronym; | ||
data['Abbrev'] = this.abbrev; | ||
if (this.glossDef != null) { | ||
data['GlossDef'] = this.glossDef.toJson(); | ||
} | ||
data['GlossSee'] = this.glossSee; | ||
return data; | ||
} | ||
} | ||
|
||
class GlossDef { | ||
String para; | ||
List<String> glossSeeAlso; | ||
|
||
GlossDef({this.para, this.glossSeeAlso}); | ||
|
||
GlossDef.fromJson(Map<String, dynamic> json) { | ||
para = json['para']; | ||
glossSeeAlso = json['GlossSeeAlso'].cast<String>(); | ||
} | ||
|
||
Map<String, dynamic> toJson() { | ||
final Map<String, dynamic> data = new Map<String, dynamic>(); | ||
data['para'] = this.para; | ||
data['GlossSeeAlso'] = this.glossSeeAlso; | ||
return data; | ||
} | ||
} |