Files
ruby/test/psych/test_boolean.rb
Hiroshi SHIBATAandClaude Opus 4.8 a02377fee8 [ruby/psych] Add positive tests for the libfyaml backend
Cover what the libfyaml backend does distinctly, each placed with the
concern it belongs to: the Psych::BACKEND and Psych.libfyaml_version
checks in test_psych.rb, the YAML 1.2 "Norway problem" boolean case in
test_boolean.rb, and the "1.1 booleans are not quoted" emission case in
test_string.rb. The 1.2 assertions are skipped on the default libyaml
backend so the same suite passes under both.

https://github.com/ruby/psych/commit/35d0a437a2

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-02 02:40:16 +00:00

63 lines
1.9 KiB
Ruby

# frozen_string_literal: true
require_relative 'helper'
module Psych
###
# Test booleans from YAML spec:
# http://yaml.org/type/bool.html
class TestBoolean < TestCase
# true/false are booleans in both YAML 1.1 and 1.2.
%w{ true True TRUE }.each do |truth|
define_method(:"test_#{truth}") do
assert_equal true, Psych.load("--- #{truth}")
end
end
%w{ false False FALSE }.each do |truth|
define_method(:"test_#{truth}") do
assert_equal false, Psych.load("--- #{truth}")
end
end
# yes/on and no/off are booleans only under YAML 1.1 (the libyaml backend).
# The YAML 1.2 libfyaml backend keeps them as plain strings.
%w{ yes Yes YES on On ON }.each do |truth|
define_method(:"test_#{truth}") do
assert_equal(libfyaml? ? truth : true, Psych.load("--- #{truth}"))
end
end
%w{ no No NO off Off OFF }.each do |truth|
define_method(:"test_#{truth}") do
assert_equal(libfyaml? ? truth : false, Psych.load("--- #{truth}"))
end
end
###
# YAML spec says "y" and "Y" may be used as true, but Syck treats them
# as literal strings
def test_y
assert_equal "y", Psych.load("--- y")
assert_equal "Y", Psych.load("--- Y")
end
###
# YAML spec says "n" and "N" may be used as false, but Syck treats them
# as literal strings
def test_n
assert_equal "n", Psych.load("--- n")
assert_equal "N", Psych.load("--- N")
end
###
# The "Norway problem": under YAML 1.2 (the libfyaml backend) yes/no/on/off
# are plain strings in every context, so the country code "no" no longer
# becomes false.
def test_norway_problem
omit 'libfyaml (YAML 1.2) backend only' unless libfyaml?
assert_equal({ 'country' => 'no' }, Psych.load("country: no"))
assert_equal %w[yes no on off], Psych.load("- yes\n- no\n- on\n- off\n")
end
end
end