Skip to content

Commit

Permalink
fixing a typo from swift-aws; add more tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
RockfordWei committed Jul 7, 2020
1 parent a18023f commit d5f7373
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 41 deletions.
2 changes: 1 addition & 1 deletion [email protected]
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
//

import PackageDescription
d

let package = Package(
name: "INIParser",
products: [
Expand Down
121 changes: 81 additions & 40 deletions Tests/INIParserTests/INIParserTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,49 +21,90 @@ import XCTest
@testable import INIParser

class INIParserTests: XCTestCase {
func testExample() {
let raw = """
; last modified 1 April 2017 by Rockford Wei
## This is another comment
freeVar1 = 1
freeVar2 = 2;
url = http://example.com/results?limit=10
[owner]
name = Rocky
organization = PerfectlySoft
;
[database]
server = 192.0.2.42 ; use IP address in case network name resolution is not working

port = 143
file = \"中文.dat ' ' \"
[汉化]
变量1 = 🇨🇳 ;使用utf8
变量2 = 加拿大。
[ 乱死了 ]
"""
let path = "/tmp/a.ini"

let path = "/tmp/a.ini"
do {
try raw.write(to: URL.init(fileURLWithPath: path), atomically: true, encoding: .utf8)
let ini = try INIParser(path)
XCTAssertEqual(ini.anonymousSection["freeVar1"] ?? "", "1")
XCTAssertEqual(ini.anonymousSection["freeVar2"] ?? "", "2")
XCTAssertEqual(ini.anonymousSection["url"] ?? "", "http://example.com/results?limit=10")
XCTAssertEqual(ini.sections["owner"]?["name"] ?? "", "Rocky")
XCTAssertEqual(ini.sections["owner"]?["organization"] ?? "", "PerfectlySoft")
XCTAssertEqual(ini.sections["database"]?["server"] ?? "", "192.0.2.42")
XCTAssertEqual(ini.sections["database"]?["port"] ?? "", "143")
XCTAssertEqual(ini.sections["database"]?["file"] ?? "", "\"中文.dat \' \' \"")
XCTAssertEqual(ini.sections["汉化"]?["变量1"] ?? "", "🇨🇳")
XCTAssertEqual(ini.sections["汉化"]?["变量2"] ?? "", "加拿大。")
}catch (let err) {
XCTFail(err.localizedDescription)
}
override func setUp() {
let raw =
"""
; last modified 1 April 2017 by Rockford Wei
## This is another comment
freeVar1 = 1
freeVar2 = 2;
url = http://example.com/results?limit=10
[owner]
name = Rocky
organization = PerfectlySoft
;
[database]
server = 192.0.2.42 ; use IP address in case network name resolution is not working
port = 143
file = \"中文.dat ' ' \"
[汉化]
变量1 = 🇨🇳 ;使用utf8
变量2 = 加拿大。
[ 乱死了 ]
"""
do {
try raw.write(to: URL.init(fileURLWithPath: path), atomically: true, encoding: .utf8)
}catch (let err) {
XCTFail(err.localizedDescription)
}
}

func validate(ini: INIParser) {
XCTAssertEqual(ini.anonymousSection["freeVar1"] ?? "", "1")
XCTAssertEqual(ini.anonymousSection["freeVar2"] ?? "", "2")
XCTAssertEqual(ini.anonymousSection["url"] ?? "", "http://example.com/results?limit=10")
XCTAssertEqual(ini.sections["owner"]?["name"] ?? "", "Rocky")
XCTAssertEqual(ini.sections["owner"]?["organization"] ?? "", "PerfectlySoft")
XCTAssertEqual(ini.sections["database"]?["server"] ?? "", "192.0.2.42")
XCTAssertEqual(ini.sections["database"]?["port"] ?? "", "143")
XCTAssertEqual(ini.sections["database"]?["file"] ?? "", "\"中文.dat \' \' \"")
XCTAssertEqual(ini.sections["汉化"]?["变量1"] ?? "", "🇨🇳")
XCTAssertEqual(ini.sections["汉化"]?["变量2"] ?? "", "加拿大。")
}

func testExample() {
do {
let ini = try INIParser(path)
validate(ini: ini)
}catch (let err) {
XCTFail(err.localizedDescription)
}
}

func testData() {
do {
let url = URL(fileURLWithPath: path)
let data = try Data(contentsOf: url)
let ini = try INIParser(data: data)
validate(ini: ini)
}catch (let err) {
XCTFail(err.localizedDescription)
}
}

func testString() {
do {
let url = URL(fileURLWithPath: path)
let data = try Data(contentsOf: url)
let string = String(data: data, encoding: .utf8) ?? "fault"
let ini = try INIParser(string: string)
validate(ini: ini)
}catch (let err) {
XCTFail(err.localizedDescription)
}
}

override func tearDown() {
unlink(path)
}

static var allTests = [
("testExample", testExample),
]
static var allTests = [
("testExample", testExample),
("testData", testData),
("testString", testString)
]
}

0 comments on commit d5f7373

Please sign in to comment.