Skip to content

Commit

Permalink
Fix libyaml 0.2.1 incompatibility
Browse files Browse the repository at this point in the history
libyaml 0.2.1 removed the errorneously written document end marker (`...`) after some scalars in root context (see yaml/libyaml#18).
Earlier libyaml releases still write the document end marker and this is hard to fix on Crystal's side. So we just ignore it and adopt the specs accordingly to coincide with the used libyaml version.
  • Loading branch information
straight-shoota committed Jun 28, 2018
1 parent df5f46a commit 97511bb
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 7 deletions.
18 changes: 14 additions & 4 deletions spec/std/yaml/builder_spec.cr
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
require "spec"
require "yaml"

private def assert_built(expected)
private def assert_built(expected, expect_document_end = false)
# libyaml 0.2.1 removed the errorneously written document end marker (`...`) after some scalars in root context (see https://github.com/yaml/libyaml/pull/18).
# Earlier libyaml releases still write the document end marker and this is hard to fix on Crystal's side.
# So we just ignore it and adopt the specs accordingly to coincide with the used libyaml version.
if expect_document_end
major, minor, _ = YAML.libyaml_version
if major == 0 && minor < 2
expected += "...\n"
end
end

string = YAML.build do |yaml|
with yaml yield yaml
end
Expand All @@ -10,7 +20,7 @@ end

describe YAML::Builder do
it "writes scalar" do
assert_built("--- 1\n...\n") do
assert_built("--- 1\n", expect_document_end: true) do
scalar(1)
end
end
Expand All @@ -22,13 +32,13 @@ describe YAML::Builder do
end

it "writes scalar with tag" do
assert_built(%(--- !foo 1\n...\n)) do
assert_built(%(--- !foo 1\n), expect_document_end: true) do
scalar(1, tag: "!foo")
end
end

it "writes scalar with anchor" do
assert_built(%(--- &foo 1\n...\n)) do
assert_built(%(--- &foo 1\n), expect_document_end: true) do
scalar(1, anchor: "foo")
end
end
Expand Down
18 changes: 15 additions & 3 deletions spec/std/yaml/serialization_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,18 @@ end

alias YamlRec = Int32 | Array(YamlRec) | Hash(YamlRec, YamlRec)

# libyaml 0.2.1 removed the errorneously written document end marker (`...`) after some scalars in root context (see https://github.com/yaml/libyaml/pull/18).
# Earlier libyaml releases still write the document end marker and this is hard to fix on Crystal's side.
# So we just ignore it and adopt the specs accordingly to coincide with the used libyaml version.
private def assert_yaml_document_end(actual, expected)
major, minor, _ = YAML.libyaml_version
if major == 0 && minor < 2
expected += "...\n"
end

actual.should eq(expected)
end

describe "YAML serialization" do
describe "from_yaml" do
it "does Nil#from_yaml" do
Expand Down Expand Up @@ -296,17 +308,17 @@ describe "YAML serialization" do

it "does for utc time" do
time = Time.utc(2010, 11, 12, 1, 2, 3)
time.to_yaml.should eq("--- 2010-11-12 01:02:03\n...\n")
assert_yaml_document_end(time.to_yaml, "--- 2010-11-12 01:02:03\n")
end

it "does for time at date" do
time = Time.utc(2010, 11, 12)
time.to_yaml.should eq("--- 2010-11-12\n...\n")
assert_yaml_document_end(time.to_yaml, "--- 2010-11-12\n")
end

it "does for utc time with nanoseconds" do
time = Time.utc(2010, 11, 12, 1, 2, 3, nanosecond: 456_000_000)
time.to_yaml.should eq("--- 2010-11-12 01:02:03.456000000\n...\n")
assert_yaml_document_end(time.to_yaml, "--- 2010-11-12 01:02:03.456000000\n")
end

it "does for bytes" do
Expand Down
6 changes: 6 additions & 0 deletions src/yaml.cr
Original file line number Diff line number Diff line change
Expand Up @@ -157,4 +157,10 @@ module YAML
def self.dump(object, io : IO)
object.to_yaml(io)
end

# Returns the used version of `libyaml`.
def self.libyaml_version : {Int32, Int32, Int32}
LibYAML.yaml_get_version(out major, out minor, out patch)
{major, minor, patch}
end
end
2 changes: 2 additions & 0 deletions src/yaml/lib_yaml.cr
Original file line number Diff line number Diff line change
Expand Up @@ -143,4 +143,6 @@ lib LibYAML
fun yaml_emitter_emit(emitter : Emitter*, event : Event*) : Int
fun yaml_emitter_delete(emitter : Emitter*)
fun yaml_emitter_flush(emitter : Emitter*) : Int

fun yaml_get_version(major : LibC::Int*, minor : LibC::Int*, patch : LibC::Int*)
end

0 comments on commit 97511bb

Please sign in to comment.