Change `Emitter` to allow plain style scalar

This commit is contained in:
Norio Nomura 2017-01-30 21:15:33 +09:00
parent 41fc301f31
commit 270e8df8b0
No known key found for this signature in database
GPG Key ID: D4A7318EB7F7138D
2 changed files with 25 additions and 30 deletions

View File

@ -256,7 +256,6 @@ extension Emitter {
assert(node.isScalar) // swiftlint:disable:next force_unwrapping
let scalar = node.scalar!
var value = scalar.string.utf8CString, tag = node.tag.name.rawValue.utf8CString
let implicit: Int32 = node.tag.name == .str ? 1 : 0
let scalar_style = yaml_scalar_style_t(rawValue: scalar.style.rawValue)
var event = yaml_event_t()
_ = value.withUnsafeMutableBytes { value in
@ -267,8 +266,8 @@ extension Emitter {
tag.baseAddress?.assumingMemoryBound(to: UInt8.self),
value.baseAddress?.assumingMemoryBound(to: UInt8.self),
Int32(value.count - 1),
0,
implicit,
1,
1,
scalar_style)
}
}

View File

@ -14,13 +14,13 @@ class EmitterTests: XCTestCase {
func testScalar() throws {
var node: Node = "key"
let expectedAnyAndPlainIsSingleQuoted = "'key'\n"
let expectedAnyAndPlain = "key\n...\n"
node.scalar?.style = .any
XCTAssertEqual(try Yams.serialize(node: node), expectedAnyAndPlainIsSingleQuoted)
XCTAssertEqual(try Yams.serialize(node: node), expectedAnyAndPlain)
node.scalar?.style = .plain
XCTAssertEqual(try Yams.serialize(node: node), expectedAnyAndPlainIsSingleQuoted)
XCTAssertEqual(try Yams.serialize(node: node), expectedAnyAndPlain)
node.scalar?.style = .singleQuoted
XCTAssertEqual(try Yams.serialize(node: node), expectedAnyAndPlainIsSingleQuoted)
XCTAssertEqual(try Yams.serialize(node: node), "'key'\n")
node.scalar?.style = .doubleQuoted
XCTAssertEqual(try Yams.serialize(node: node), "\"key\"\n")
@ -34,9 +34,9 @@ class EmitterTests: XCTestCase {
var node: Node = ["a", "b", "c"]
let expectedAnyIsBlock = [
"- 'a'",
"- 'b'",
"- 'c'",
"- a",
"- b",
"- c",
""
].joined(separator: "\n")
node.sequence?.style = .any
@ -45,15 +45,15 @@ class EmitterTests: XCTestCase {
XCTAssertEqual(try Yams.serialize(node: node), expectedAnyIsBlock)
node.sequence?.style = .flow
XCTAssertEqual(try Yams.serialize(node: node), "['a', 'b', 'c']\n")
XCTAssertEqual(try Yams.serialize(node: node), "[a, b, c]\n")
}
func testMapping() throws {
var node: Node = ["key1": "value1", "key2": "value2"]
let expectedAnyIsBlock = [
"'key1': 'value1'",
"'key2': 'value2'",
"key1: value1",
"key2: value2",
""
].joined(separator: "\n")
node.mapping?.style = .any
@ -62,26 +62,22 @@ class EmitterTests: XCTestCase {
XCTAssertEqual(try Yams.serialize(node: node), expectedAnyIsBlock)
node.mapping?.style = .flow
XCTAssertEqual(try Yams.serialize(node: node), "{'key1': 'value1', 'key2': 'value2'}\n")
XCTAssertEqual(try Yams.serialize(node: node), "{key1: value1, key2: value2}\n")
}
func testLineBreaks() throws {
let node: Node = "key"
do {
let yaml = try Yams.serialize(node: node, lineBreak: .ln)
let expected = "'key'\n"
XCTAssertEqual(yaml, expected)
}
do {
let yaml = try Yams.serialize(node: node, lineBreak: .cr)
let expected = "'key'\r"
XCTAssertEqual(yaml, expected)
}
do {
let yaml = try Yams.serialize(node: node, lineBreak: .crln)
let expected = "'key'\r\n"
XCTAssertEqual(yaml, expected)
}
let expected = [
"key",
"...",
""
]
XCTAssertEqual(try Yams.serialize(node: node, lineBreak: .ln),
expected.joined(separator: "\n"))
XCTAssertEqual(try Yams.serialize(node: node, lineBreak: .cr),
expected.joined(separator: "\r"))
XCTAssertEqual(try Yams.serialize(node: node, lineBreak: .crln),
expected.joined(separator: "\r\n"))
}
func testAllowUnicode() throws {
@ -94,7 +90,7 @@ class EmitterTests: XCTestCase {
}
do {
let yaml = try Yams.serialize(node: node, allowUnicode: true)
let expected = "'あ'\n"
let expected = "\n...\n"
XCTAssertEqual(yaml, expected)
}
}