Skip to content

Commit

Permalink
Parse quoted values as strings
Browse files Browse the repository at this point in the history
Fix #116
  • Loading branch information
norio-nomura committed Mar 27, 2018
1 parent 2dddbfe commit 8dd1904
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 5 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
5 changes: 2 additions & 3 deletions Sources/Yams/Parser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
62 changes: 62 additions & 0 deletions Tests/YamsTests/ConstructorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -429,6 +490,7 @@ extension ConstructorTests {
("testNull", testNull),
("testOmap", testOmap),
("testPairs", testPairs),
("testQuotationMark", testQuotationMark),
("testSet", testSet),
("testSeq", testSeq),
("testTimestamp", testTimestamp),
Expand Down
2 changes: 1 addition & 1 deletion Tests/YamsTests/YamlErrorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 8dd1904

Please sign in to comment.