File: test_paper.rb

package info (click to toggle)
ruby-cairo 1.17.13-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,532 kB
  • sloc: ruby: 11,997; ansic: 10,183; sh: 48; makefile: 4
file content (81 lines) | stat: -rw-r--r-- 2,308 bytes parent folder | download | duplicates (8)
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
require 'cairo'

class PaperTest < Test::Unit::TestCase
  def test_parse_paper
    a4 = Cairo::Paper::A4
    assert_parse(a4, a4)
  end

  def test_parse_name
    assert_parse(:A4, "A4")
    assert_parse(:A4_LANDSCAPE, "A4 landscape")
    assert_parse(:A4, :A4)
    assert_parse(:A4, :a4)

    assert_parse(:JAPANESE_POSTCARD, :japanese_postcard)
    assert_parse(:JAPANESE_POSTCARD, "japanese-postcard")
    assert_parse(:JAPANESE_POSTCARD, "Japanese postcard")

    exception = assert_raise(Cairo::Paper::UnknownPaperName) do
      Cairo::Paper.parse(:nonexistence)
    end
    assert_equal(:nonexistence, exception.name)
  end

  def test_parse_size
    assert_parse(paper(100, 200), "100x200")
    assert_parse(paper(283.46456664, 566.929134), "100mmx200mm")
    assert_parse(paper(284.88188952, 200.9), "100.5mmx200.9")
    assert_parse(paper(72, 612.0), "1inx8.5inch")
    assert_parse(paper(28.346456664, 24094.488168), "1cmx8.5m")
    assert_parse(paper(100, 200), [100, 200])

    exception = assert_raise(Cairo::Paper::UnknownUnit) do
      Cairo::Paper.parse("100kmx100")
    end
    assert_equal("km", exception.unit)
  end

  def test_parse_size_with_name
    assert_parse(paper(28.346456664, 24094.488168, nil, "Name"),
                 "1cmx8.5m#Name")
  end

  def test_unrecognized_input
    assert_nothing_raised do
      Cairo::Paper.parse({})
    end

    exception = assert_raise(Cairo::Paper::UnrecognizedPaperDescription) do
      Cairo::Paper.parse({}, true)
    end
    assert_equal({}, exception.description)
  end

  def test_unit
    paper = parse("1cmx8.5m")
    assert_nil(paper.unit)
    assert_in_delta(28.346456664, 0.01, paper.width)
    assert_in_delta(24094.488168, 0.01, paper.height)

    paper.unit = "inch"
    assert_equal("inch", paper.unit)
    assert_in_delta(0.393700787, 0.01, paper.width)
    assert_in_delta(334.645669, 0.01, paper.height)
  end

  private
  def paper(width, height, *rest)
    Cairo::Paper.new(width, height, *rest)
  end

  def parse(paper_description)
    Cairo::Paper.parse(paper_description)
  end

  def assert_parse(expected, paper_description, message=nil)
    expected = Cairo::Paper.const_get(expected) if expected.is_a?(Symbol)
    actual_paper = parse(paper_description)
    assert_equal(expected, actual_paper, message)
  end
end