diff --git a/CHANGELOG.md b/CHANGELOG.md index f3172268..3867ee79 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,7 +11,9 @@ ##### Bug Fixes -* None. +* Fix parse quoted values as strings. + [Norio Nomura](https://github.com/norio-nomura) + [#105](https://github.com/jpsim/Yams/issues/116) ## 0.6.0 diff --git a/Sources/Yams/Parser.swift b/Sources/Yams/Parser.swift index c006e007..a214d071 100644 --- a/Sources/Yams/Parser.swift +++ b/Sources/Yams/Parser.swift @@ -287,9 +287,8 @@ private class Event { return Node.Scalar.Style(rawValue: event.data.scalar.style.rawValue)! } var scalarTag: String? { - guard event.data.scalar.plain_implicit == 0, - event.data.scalar.quoted_implicit == 0 else { - return nil + if event.data.scalar.quoted_implicit == 1 { + return Tag.Name.str.rawValue } return string(from: event.data.scalar.tag) } diff --git a/Tests/YamsTests/ConstructorTests.swift b/Tests/YamsTests/ConstructorTests.swift index 143155f5..46193efa 100644 --- a/Tests/YamsTests/ConstructorTests.swift +++ b/Tests/YamsTests/ConstructorTests.swift @@ -287,6 +287,67 @@ class ConstructorTests: XCTestCase { // swiftlint:disable:this type_body_length YamsAssertEqual(objects, expected) } + func testQuotationMark() throws { + // swiftlint:disable line_length + // ```terminal.sh-session + // $ python + // Python 3.6.4 (default, Mar 16 2018, 17:10:15) + // [GCC 4.2.1 Compatible Apple LLVM 9.1.0 (clang-902.0.37.1)] on darwin + // Type "help", "copyright", "credits" or "license" for more information. + // >>> import yaml + // >>> yaml.load(r"""plain: 10.10 + // ... single quote: '10.10' + // ... double quote: "10.10" + // ... literal: | + // ... 10.10 + // ... literal single quote: | + // ... '10.10' + // ... literal double quote: | + // ... "10.10" + // ... folded: > + // ... 10.10 + // ... folded single quote: > + // ... '10.10' + // ... folded double quote: > + // ... "10.10" + // ... """) + // {'plain': 10.1, 'single quote': '10.10', 'double quote': '10.10', 'literal': '10.10\n', 'literal single quote': "'10.10'\n", 'literal double quote': '"10.10"\n', 'folded': '10.10\n', 'folded single quote': "'10.10'\n", 'folded double quote': '"10.10"\n'} + // >>> + // ``` + // swiftlint:enable line_length + let example = """ + plain: 10.10 + single quote: '10.10' + double quote: "10.10" + literal: | + 10.10 + literal single quote: | + '10.10' + literal double quote: | + "10.10" + folded: > + 10.10 + folded single quote: > + '10.10' + folded double quote: > + "10.10" + + """ + let objects = try Yams.load(yaml: example) + let expected: [String: Any] = [ + "plain": 10.10, + "single quote": "10.10", + "double quote": "10.10", + "literal": "10.10\n", + "literal single quote": "'10.10'\n", + "literal double quote": "\"10.10\"\n", + "folded": "10.10\n", + "folded single quote": "'10.10'\n", + "folded double quote": "\"10.10\"\n" + ] + YamsAssertEqual(objects, expected) + } + func testSet() throws { let example = """ # Explicitly typed set. @@ -429,6 +490,7 @@ extension ConstructorTests { ("testNull", testNull), ("testOmap", testOmap), ("testPairs", testPairs), + ("testQuotationMark", testQuotationMark), ("testSet", testSet), ("testSeq", testSeq), ("testTimestamp", testTimestamp), diff --git a/Tests/YamsTests/YamlErrorTests.swift b/Tests/YamsTests/YamlErrorTests.swift index 3c122c23..7c7c1d35 100644 --- a/Tests/YamsTests/YamlErrorTests.swift +++ b/Tests/YamsTests/YamlErrorTests.swift @@ -65,7 +65,7 @@ class YamlErrorTests: XCTestCase { let parser = try Parser(yaml: invalidYAML) // first iteration returns scalar - XCTAssertEqual(try parser.nextRoot(), Node("", Tag(.null), .literal)) + XCTAssertEqual(try parser.nextRoot(), Node("", Tag(.str), .literal)) // second iteration throws error XCTAssertThrowsError(try parser.nextRoot()) { error in XCTAssertTrue(error is YamlError)