summaryrefslogtreecommitdiffhomepage
path: root/test/text_spec.rb
blob: 545451044859b69cd4194a93380de6ffc67e664e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
require 'ruby2d'

RSpec.describe Ruby2D::Text do

  describe "#new" do
    it "raises exception if font file doesn't exist" do
      expect { Text.new('hello', font: 'bad_font.ttf') }.to raise_error(Ruby2D::Error)
    end

    it "uses the system default font if one is not provided" do
      txt = Text.new('hello')
      expect(txt.font).to eq(Font.default)
    end

    it "creates text with a white filter by default" do
      txt = Text.new('hello')
      expect(txt.color).to be_a(Ruby2D::Color)
      expect(txt.color.r).to eq(1)
      expect(txt.color.g).to eq(1)
      expect(txt.color.b).to eq(1)
      expect(txt.color.a).to eq(1)
    end

    it "creates an image with options" do
      txt = Text.new(
        'hello', font: 'test/media/bitstream_vera/vera.ttf',
        x: 10, y: 20, z: 30,
        size: 40, rotate: 50,
        color: 'gray', opacity: 0.5
      )

      expect(txt.text).to eq('hello')
      expect(txt.x).to eq(10)
      expect(txt.y).to eq(20)
      expect(txt.z).to eq(30)
      expect(txt.size).to eq(40)
      expect(txt.rotate).to eq(50)
      expect(txt.color.r).to eq(2/3.0)
      expect(txt.opacity).to eq(0.5)
    end
  end

  describe "attributes" do
    it "can be set and read" do
      txt = Text.new('hello')
      txt.x = 10
      txt.y = 20
      txt.z = 30
      txt.size = 40
      txt.rotate = 50
      txt.color = 'gray'
      txt.opacity = 0.5

      expect(txt.x).to eq(10)
      expect(txt.y).to eq(20)
      expect(txt.z).to eq(30)
      expect(txt.size).to eq(40)
      expect(txt.rotate).to eq(50)
      expect(txt.color.r).to eq(2/3.0)
      expect(txt.opacity).to eq(0.5)
    end
  end

  describe "#text=" do
    it "maps Time to string" do
      txt = Text.new('hello', font: 'test/media/bitstream_vera/vera.ttf')
      txt.text = Time.new(1, 1, 1, 1, 1, 1, 1)
      expect(txt.text).to eq('0001-01-01 01:01:01 +0000')
    end

    it "maps Number to string" do
      txt = Text.new('hello', font: 'test/media/bitstream_vera/vera.ttf')
      txt.text = 0
      expect(txt.text).to eq('0')
    end
  end

  describe "#width" do
    it "is known after creation" do
      txt = Text.new('Hello Ruby!', font: 'test/media/bitstream_vera/vera.ttf')
      expect(txt.width).to be_between(110, 120)
    end

    it "is known after updating" do
      txt = Text.new('hello', font: 'test/media/bitstream_vera/vera.ttf')
      txt.text = 'Hello!'
      expect(txt.width).to eq(59)
    end
  end

  describe "#height" do
    it "is known after creation" do
      txt = Text.new('hello', font: 'test/media/bitstream_vera/vera.ttf')
      expect(txt.height).to eq(24)
    end

    it "is known after updating" do
      txt = Text.new('hello', font: 'test/media/bitstream_vera/vera.ttf')
      txt.text = 'Good morning world!'
      expect(txt.height).to eq(24)
    end
  end

  describe "#contains?" do
    it "returns true if point is inside the text" do
      txt = Text.new('hello', font: 'test/media/bitstream_vera/vera.ttf')
      txt.text = 'Hello world!'
      expect(txt.contains?(txt.width / 2, txt.height / 2)).to be true
    end

    it "returns false if point is outside the text" do
      txt = Text.new('hello', font: 'test/media/bitstream_vera/vera.ttf')
      txt.text = 'Hello world!'
      expect(txt.contains?(  - txt.width / 2,     txt.height / 2)).to be false
      expect(txt.contains?(    txt.width / 2,   - txt.height / 2)).to be false
      expect(txt.contains?(3 * txt.width / 2,     txt.height / 2)).to be false
      expect(txt.contains?(    txt.width / 2, 3 * txt.height / 2)).to be false
    end
  end

end