Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix parse quoted values as strings #117

Merged
merged 1 commit into from
Mar 29, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
[#116](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? {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can return a String now

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It can't since string(from:) still returns 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