1#!/usr/bin/env python
2# Copyright (c) 2012 The Chromium Authors. All rights reserved.
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5
6"""Tests for strip_js_comments module."""
7
8import unittest
9
10from py_vulcanize import strip_js_comments
11
12
13# This test case tests a protected method.
14# pylint: disable=W0212
15class JavaScriptStripCommentTests(unittest.TestCase):
16  """Test case for _strip_js_comments and _TokenizeJS."""
17
18  def test_strip_comments(self):
19    self.assertEquals(
20        'A ', strip_js_comments.StripJSComments('A // foo'))
21    self.assertEquals(
22        'A bar', strip_js_comments.StripJSComments('A // foo\nbar'))
23    self.assertEquals(
24        'A  b', strip_js_comments.StripJSComments('A /* foo */ b'))
25    self.assertEquals(
26        'A  b', strip_js_comments.StripJSComments('A /* foo\n */ b'))
27
28  def test_tokenize_empty(self):
29    tokens = list(strip_js_comments._TokenizeJS(''))
30    self.assertEquals([], tokens)
31
32  def test_tokenize_nl(self):
33    tokens = list(strip_js_comments._TokenizeJS('\n'))
34    self.assertEquals(['\n'], tokens)
35
36  def test_tokenize_slashslash_comment(self):
37    tokens = list(strip_js_comments._TokenizeJS('A // foo'))
38    self.assertEquals(['A ', '//', ' foo'], tokens)
39
40  def test_tokenize_slashslash_comment_then_newline(self):
41    tokens = list(strip_js_comments._TokenizeJS('A // foo\nbar'))
42    self.assertEquals(['A ', '//', ' foo', '\n', 'bar'], tokens)
43
44  def test_tokenize_cstyle_comment_one_line(self):
45    tokens = list(strip_js_comments._TokenizeJS('A /* foo */'))
46    self.assertEquals(['A ', '/*', ' foo ', '*/'], tokens)
47
48  def test_tokenize_cstyle_comment_multi_line(self):
49    tokens = list(strip_js_comments._TokenizeJS('A /* foo\n*bar\n*/'))
50    self.assertEquals(['A ', '/*', ' foo', '\n', '*bar', '\n', '*/'], tokens)
51
52
53if __name__ == '__main__':
54  unittest.main()
55