Runtime assertions for Ruby
literal.fun
ruby
1# frozen_string_literal: true
2
3Example = Literal::Object
4
5test "no writer by default" do
6 example = Class.new(Example) do
7 prop :example, String
8 end
9
10 object = example.new(
11 example: "hello",
12 )
13
14 refute object.public_methods.include?(:example=)
15 refute object.protected_methods.include?(:example=)
16 refute object.private_methods.include?(:example=)
17end
18
19test "false writer" do
20 example = Class.new(Example) do
21 prop :example, String, writer: false
22 end
23
24 object = example.new(
25 example: "hello",
26 )
27
28 refute object.public_methods.include?(:example)
29 refute object.protected_methods.include?(:example)
30 refute object.private_methods.include?(:example)
31end
32
33test "private writer" do
34 example = Class.new(Example) do
35 prop :example, String, writer: :private, reader: :public
36 end
37
38 object = example.new(
39 example: "hello",
40 )
41
42 assert object.public_methods.include?(:example)
43 assert_equal object.__send__(:example), "hello"
44end
45
46test "protected writer" do
47 example = Class.new(Example) do
48 prop :example, String, writer: :protected, reader: :public
49 end
50
51 object = example.new(
52 example: "hello",
53 )
54
55 assert object.protected_methods.include?(:example=)
56 assert_equal object.__send__(:example=, "world"), "world"
57end
58
59test "public writer" do
60 example = Class.new(Example) do
61 prop :example, String, writer: :public, reader: :public
62 end
63
64 object = example.new(
65 example: "hello",
66 )
67
68 assert object.public_methods.include?(:example=)
69 assert_equal object.example = "world", "world"
70end