summaryrefslogtreecommitdiffstats
path: root/devtools/client/shared/test/unit/test_parseSingleValue.js
blob: 73e4f0ac4fa900f275ddc6d06d98a3e4c9d7fbbf (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
/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */
/* vim: set ts=2 et sw=2 tw=80: */
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

var Cu = Components.utils;
const {require} = Cu.import("resource://devtools/shared/Loader.jsm", {});
const {parseSingleValue} = require("devtools/shared/css/parsing-utils");
const {isCssPropertyKnown} = require("devtools/server/actors/css-properties");

const TEST_DATA = [
  {input: null, throws: true},
  {input: undefined, throws: true},
  {input: "", expected: {value: "", priority: ""}},
  {input: "  \t \t \n\n  ", expected: {value: "", priority: ""}},
  {input: "blue", expected: {value: "blue", priority: ""}},
  {input: "blue !important", expected: {value: "blue", priority: "important"}},
  {input: "blue!important", expected: {value: "blue", priority: "important"}},
  {input: "blue ! important", expected: {value: "blue", priority: "important"}},
  {
    input: "blue !  important",
    expected: {value: "blue", priority: "important"}
  },
  {input: "blue !", expected: {value: "blue", priority: ""}},
  {input: "blue !mportant", expected: {value: "blue !mportant", priority: ""}},
  {
    input: "  blue   !important ",
    expected: {value: "blue", priority: "important"}
  },
  {
    input: "url(\"http://url.com/whyWouldYouDoThat!important.png\") !important",
    expected: {
      value: "url(\"http://url.com/whyWouldYouDoThat!important.png\")",
      priority: "important"
    }
  },
  {
    input: "url(\"http://url.com/whyWouldYouDoThat!important.png\")",
    expected: {
      value: "url(\"http://url.com/whyWouldYouDoThat!important.png\")",
      priority: ""
    }
  },
  {
    input: "\"content!important\" !important",
    expected: {
      value: "\"content!important\"",
      priority: "important"
    }
  },
  {
    input: "\"content!important\"",
    expected: {
      value: "\"content!important\"",
      priority: ""
    }
  },
  {
    input: "\"all the \\\"'\\\\ special characters\"",
    expected: {
      value: "\"all the \\\"'\\\\ special characters\"",
      priority: ""
    }
  }
];

function run_test() {
  for (let test of TEST_DATA) {
    do_print("Test input value " + test.input);
    try {
      let output = parseSingleValue(isCssPropertyKnown, test.input);
      assertOutput(output, test.expected);
    } catch (e) {
      do_print("parseSingleValue threw an exception with the given input " +
        "value");
      if (test.throws) {
        do_print("Exception expected");
        do_check_true(true);
      } else {
        do_print("Exception unexpected\n" + e);
        do_check_true(false);
      }
    }
  }
}

function assertOutput(actual, expected) {
  do_print("Check that the output has the expected value and priority");
  do_check_eq(expected.value, actual.value);
  do_check_eq(expected.priority, actual.priority);
}