summaryrefslogtreecommitdiffhomepage
path: root/test/drawing/tc_line_series.rb
blob: 0add56eeb218bc52a8098875a6e000273fcfc1c9 (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
# frozen_string_literal: true

require 'tc_helper'

class TestLineSeries < Test::Unit::TestCase
  def setup
    p = Axlsx::Package.new
    @ws = p.workbook.add_worksheet name: "hmmm"
    chart = @ws.add_chart Axlsx::Line3DChart, title: "fishery"
    @series = chart.add_series(
      data: [0, 1, 2],
      labels: ["zero", "one", "two"],
      title: "bob",
      color: "#FF0000",
      show_marker: true,
      smooth: true
    )
  end

  def test_initialize
    assert_equal("bob", @series.title.text, "series title has been applied")
    assert_equal(@series.labels.class, Axlsx::AxDataSource)
    assert_equal(@series.data.class, Axlsx::NumDataSource)
  end

  def test_show_marker
    assert(@series.show_marker)
    @series.show_marker = false

    refute(@series.show_marker)
  end

  def test_smooth
    assert(@series.smooth)
    @series.smooth = false

    refute(@series.smooth)
  end

  def test_marker_symbol
    assert_equal(:default, @series.marker_symbol)
    @series.marker_symbol = :circle

    assert_equal(:circle, @series.marker_symbol)
  end

  def test_to_xml_string
    doc = Nokogiri::XML(wrap_with_namespaces(@series))

    assert(doc.xpath("//srgbClr[@val='#{@series.color}']"))
    assert_equal(0, xpath_with_namespaces(doc, "//c:marker").size)
    assert(doc.xpath("//smooth"))

    @series.marker_symbol = :diamond
    doc = Nokogiri::XML(wrap_with_namespaces(@series))

    assert_equal(1, xpath_with_namespaces(doc, "//c:marker/c:symbol[@val='diamond']").size)

    @series.show_marker = false
    doc = Nokogiri::XML(wrap_with_namespaces(@series))

    assert_equal(1, xpath_with_namespaces(doc, "//c:marker/c:symbol[@val='none']").size)
  end

  def wrap_with_namespaces(series)
    +'<c:chartSpace xmlns:c="' <<
      Axlsx::XML_NS_C <<
      '" xmlns:a="' <<
      Axlsx::XML_NS_A <<
      '">' <<
      series.to_xml_string <<
      '</c:chartSpace>'
  end

  def xpath_with_namespaces(doc, xpath)
    doc.xpath(xpath, "a" => Axlsx::XML_NS_A, "c" => Axlsx::XML_NS_C)
  end
end