Skip to content

Commit

Permalink
Update unittests
Browse files Browse the repository at this point in the history
  • Loading branch information
FreeSlave committed Aug 21, 2015
1 parent dc947d1 commit 595ae57
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 26 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ bin/
lib/
docs/
dub.selections.json
*.lst
78 changes: 52 additions & 26 deletions source/inilike.d
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,17 @@ private alias KeyValueTuple = Tuple!(string, "key", string, "value");
return cache;
}

///
unittest
{
environment["LANG"] = "ru_RU";
assert(currentLocale() == "ru_RU");
environment["LC_ALL"] = "de_DE";
assert(currentLocale() == "de_DE");
environment["LC_CTYPE"] = "fr_BE";
assert(currentLocale() == "fr_BE");
}

/**
* Make locale name based on language, country, encoding and modifier.
* Returns: locale name in form lang_COUNTRY.ENCODING@MODIFIER
Expand Down Expand Up @@ -437,6 +448,29 @@ public:
return value(key, defaultValue);
}

///
unittest
{
auto lilf = new IniLikeFile;
lilf.addGroup("Entry");
auto group = lilf.group("Entry");
assert(group.name == "Entry");
group["Name"] = "Programmer";
group["Name[ru_RU]"] = "Разработчик";
group["Name[ru@jargon]"] = "Кодер";
group["Name[ru]"] = "Программист";
group["Name[de_DE@dialect]"] = "Programmierer"; //just example
group["GenericName"] = "Program";
group["GenericName[ru]"] = "Программа";
assert(group["Name"] == "Programmer");
assert(group.localizedValue("Name", "ru@jargon") == "Кодер");
assert(group.localizedValue("Name", "ru_RU@jargon") == "Разработчик");
assert(group.localizedValue("Name", "ru") == "Программист");
assert(group.localizedValue("Name", "nonexistent locale") == "Programmer");
assert(group.localizedValue("Name", "de_DE@dialect") == "Programmierer");
assert(group.localizedValue("GenericName", "ru_RU") == "Программа");
}

/**
* Same as localized version of opIndexAssign, but uses function syntax.
*/
Expand Down Expand Up @@ -567,7 +601,7 @@ public:
{
noOptions = 0, /// Read all groups and skip comments and empty lines.
firstGroupOnly = 1, /// Ignore other groups than the first one.
preserveComments = 2, /// Preserve comments and empty lines. Use this when you want to preserve them across writing.
preserveComments = 2, /// Preserve comments and empty lines. Use this when you want to keep them across writing.
ignoreGroupDuplicates = 4, /// Ignore group duplicates. The first found will be used.
ignoreInvalidKeys = 8 /// Skip invalid keys during parsing.
}
Expand Down Expand Up @@ -619,16 +653,16 @@ public:
break;
case IniLikeLine.Type.GroupStart:
{
if (currentGroup !is null && (options & ReadOptions.firstGroupOnly)) {
break;
}

if ((options & ReadOptions.ignoreGroupDuplicates) && group(line.groupName)) {
ignoreKeyValues = true;
continue;
}
ignoreKeyValues = false;
currentGroup = addGroup(line.groupName);

if (options & ReadOptions.firstGroupOnly) {
break;
}
}
break;
case IniLikeLine.Type.KeyValue:
Expand Down Expand Up @@ -796,28 +830,9 @@ private:
string[] _firstComments;
}

///
unittest
{

//Test locale matching lookup
auto lilf = new IniLikeFile;
lilf.addGroup("Entry");
auto group = lilf.group("Entry");
assert(group.name == "Entry");
group["Name"] = "Programmer";
group["Name[ru_RU]"] = "Разработчик";
group["Name[ru@jargon]"] = "Кодер";
group["Name[ru]"] = "Программист";
group["GenericName"] = "Program";
group["GenericName[ru]"] = "Программа";
assert(group["Name"] == "Programmer");
assert(group.localizedValue("Name", "ru@jargon") == "Кодер");
assert(group.localizedValue("Name", "ru_RU@jargon") == "Разработчик");
assert(group.localizedValue("Name", "ru") == "Программист");
assert(group.localizedValue("Name", "nonexistent locale") == "Programmer");
assert(group.localizedValue("GenericName", "ru_RU") == "Программа");

//Test IniLikeFile
string contents =
`# The first comment
[First Entry]
Expand All @@ -830,7 +845,8 @@ Name=Commander
Comment=Manage files
# The last comment`;

auto ilf = new IniLikeFile(iniLikeStringReader(contents), IniLikeFile.ReadOptions.preserveComments);
auto ilf = new IniLikeFile(iniLikeStringReader(contents), IniLikeFile.ReadOptions.preserveComments, "contents.ini");
assert(ilf.fileName() == "contents.ini");
assert(ilf.group("First Entry"));
assert(ilf.group("Another Group"));
assert(ilf.saveToString() == contents);
Expand Down Expand Up @@ -877,6 +893,16 @@ Comment=Manage files
static assert(is(typeof(cilf.group("First Entry").byKeyValue())));
static assert(is(typeof(cilf.group("First Entry").byIniLine())));

contents =
`[First]
Key=Value
[Second]
Key=Value`;
ilf = new IniLikeFile(iniLikeStringReader(contents), IniLikeFile.ReadOptions.firstGroupOnly);
assert(ilf.group("First") !is null);
assert(ilf.group("Second") is null);
assert(ilf.group("First")["Key"] == "Value");

contents =
`[Group]
GenericName=File manager
Expand Down

0 comments on commit 595ae57

Please sign in to comment.