|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require_relative "../../test_helper" |
| 4 | + |
| 5 | +class Phlexing::Converter::SvgTest < Minitest::Spec |
| 6 | + it "converts SVG" do |
| 7 | + html = %( |
| 8 | + <svg> |
| 9 | + <path d="123"></path> |
| 10 | + </svg> |
| 11 | + ) |
| 12 | + |
| 13 | + expected = <<~PHLEX.strip |
| 14 | + svg { |s| s.path(d: "123") } |
| 15 | + PHLEX |
| 16 | + |
| 17 | + assert_phlex_template expected, html |
| 18 | + end |
| 19 | + |
| 20 | + it "converts SVG with attributes" do |
| 21 | + html = %( |
| 22 | + <svg one="attribute" two="attributes"> |
| 23 | + <path d="123"></path> |
| 24 | + </svg> |
| 25 | + ) |
| 26 | + |
| 27 | + expected = <<~PHLEX.strip |
| 28 | + svg(one: "attribute", two: "attributes") { |s| s.path(d: "123") } |
| 29 | + PHLEX |
| 30 | + |
| 31 | + assert_phlex_template expected, html |
| 32 | + end |
| 33 | + |
| 34 | + it "converts SVG with ERB interpolation" do |
| 35 | + html = %( |
| 36 | + <svg one="<%= interpolate %>" two="<%= method_call(123) %>"> |
| 37 | + <path d="123"></path> |
| 38 | + </svg> |
| 39 | + ) |
| 40 | + |
| 41 | + expected = <<~PHLEX.strip |
| 42 | + svg(one: interpolate, two: method_call(123)) { |s| s.path(d: "123") } |
| 43 | + PHLEX |
| 44 | + |
| 45 | + assert_phlex_template expected, html do |
| 46 | + assert_locals "interpolate" |
| 47 | + assert_instance_methods "method_call" |
| 48 | + end |
| 49 | + end |
| 50 | + |
| 51 | + it "converts SVG with case-sensitive" do |
| 52 | + html = %( |
| 53 | + <svg> |
| 54 | + <feSpecularLighting> |
| 55 | + <fePointLight/> |
| 56 | + </feSpecularLighting> |
| 57 | + </svg> |
| 58 | + ) |
| 59 | + |
| 60 | + expected = <<~PHLEX.strip |
| 61 | + svg { |s| s.feSpecularLighting { s.fePointLight } } |
| 62 | + PHLEX |
| 63 | + |
| 64 | + assert_phlex_template expected, html |
| 65 | + end |
| 66 | + |
| 67 | + it "nested SVG" do |
| 68 | + html = %( |
| 69 | + <svg xmlns="http://www.w3.org/2000/svg" version="1.1" width="5cm" height="5cm"> |
| 70 | + <desc>Two groups, each of two rectangles</desc> |
| 71 | + <g id="group1" fill="red"> |
| 72 | + <rect x="1cm" y="1cm" width="1cm" height="1cm"/> |
| 73 | + <rect x="3cm" y="1cm" width="1cm" height="1cm"/> |
| 74 | + </g> |
| 75 | +
|
| 76 | + <g id="group2" fill="blue"> |
| 77 | + <rect x="1cm" y="3cm" width="1cm" height="1cm"/> |
| 78 | + <rect x="3cm" y="3cm" width="1cm" height="1cm"/> |
| 79 | + </g> |
| 80 | +
|
| 81 | + <rect x=".01cm" y=".01cm" width="4.98cm" height="4.98cm" fill="none" stroke="blue" stroke-width=".02cm"/> |
| 82 | + </svg> |
| 83 | + ) |
| 84 | + |
| 85 | + expected = <<~PHLEX.strip |
| 86 | + svg( |
| 87 | + xmlns: "http://www.w3.org/2000/svg", |
| 88 | + version: "1.1", |
| 89 | + width: "5cm", |
| 90 | + height: "5cm" |
| 91 | + ) do |s| |
| 92 | + s.desc { "Two groups, each of two rectangles" } |
| 93 | + s.g(id: "group1", fill: "red") do |
| 94 | + s.rect(x: "1cm", y: "1cm", width: "1cm", height: "1cm") |
| 95 | + s.rect(x: "3cm", y: "1cm", width: "1cm", height: "1cm") |
| 96 | + end |
| 97 | + s.g(id: "group2", fill: "blue") do |
| 98 | + s.rect(x: "1cm", y: "3cm", width: "1cm", height: "1cm") |
| 99 | + s.rect(x: "3cm", y: "3cm", width: "1cm", height: "1cm") |
| 100 | + end |
| 101 | + s.rect( |
| 102 | + x: ".01cm", |
| 103 | + y: ".01cm", |
| 104 | + width: "4.98cm", |
| 105 | + height: "4.98cm", |
| 106 | + fill: "none", |
| 107 | + stroke: "blue", |
| 108 | + stroke_width: ".02cm" |
| 109 | + ) |
| 110 | + end |
| 111 | + PHLEX |
| 112 | + |
| 113 | + assert_phlex_template expected, html |
| 114 | + end |
| 115 | +end |
0 commit comments