Skip to content

Commit

Permalink
Tweak test
Browse files Browse the repository at this point in the history
  • Loading branch information
marcandre committed Mar 7, 2023
1 parent fc41668 commit c729530
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions test/data_test.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
# -*- coding: us-ascii -*-
# frozen_string_literal: false
require './test/test_helper'
require 'backports/3.2.0/data'
require 'backports/2.5.0/hash/slice'

class TestData < Test::Unit::TestCase
def test_define
klass = Data.define(:foo, :bar)
assert_kind_of(Class, klass)
assert_equal(%i[foo bar], klass.members)

assert_raise(NoMethodError) { Data.new(:foo) }
assert_raise(NoMethodError) { Data.new(:foo) } unless RUBY_VERSION < '2.7'
assert_raise(TypeError) { Data.define(0) }

# Because some code is shared with Struct, check we don't share unnecessary functionality
Expand Down Expand Up @@ -40,7 +41,7 @@ def test_define_edge_cases
assert_same(x, o.b!)

assert_raise(ArgumentError) { Data.define(:x=) }
assert_raise(ArgumentError, /duplicate member/) { Data.define(:x, :x) }
assert_raise(ArgumentError) { Data.define(:x, :x) }
end

def test_define_with_block
Expand Down Expand Up @@ -113,7 +114,7 @@ def test_instance_behavior
assert_equal([], test.method(:foo).parameters)

assert_equal({foo: 1, bar: 2}, test.to_h)
assert_equal({"foo"=>"1", "bar"=>"2"}, test.to_h { [_1.to_s, _2.to_s] })
assert_equal({"foo"=>"1", "bar"=>"2"}, test.to_h { [_1.to_s, _2.to_s] }) unless RUBY_VERSION < '2.7'

assert_equal({foo: 1, bar: 2}, test.deconstruct_keys(nil))
assert_equal({foo: 1}, test.deconstruct_keys(%i[foo]))
Expand All @@ -132,9 +133,9 @@ def test_inspect
assert_equal("#<data Foo a=1>", o.inspect)
Object.instance_eval { remove_const(:Foo) }

klass = Data.define(:@a)
o = klass.new(1)
assert_equal("#<data :@a=1>", o.inspect)
# klass = Data.define(:@a)
# o = klass.new(1)
# assert_equal("#<data :@a=1>", o.inspect)
end

def test_equal
Expand Down Expand Up @@ -199,6 +200,7 @@ def test_with
assert_equal(test, klass.new(foo: 10, bar: 20))

# Wrong protocol
unless RUBY_VERSION < '2.3' # assert_raise_with_message buggy?
assert_raise_with_message(ArgumentError, "wrong number of arguments (given 1, expected 0)") do
source.with(10)
end
Expand All @@ -213,7 +215,8 @@ def test_with
end
assert_raise_with_message(ArgumentError, "wrong number of arguments (given 1, expected 0)") do
source.with({ bar: 2 })
end
end unless RUBY_VERSION < '3'
end
end

def test_memberless
Expand Down

0 comments on commit c729530

Please sign in to comment.