1# Copyright 2015 The Chromium Authors. All rights reserved. 2# Use of this source code is governed by a BSD-style license that can be 3# found in the LICENSE file. 4 5import unittest 6 7from catapult_build import js_checks 8 9 10class JsChecksTest(unittest.TestCase): 11 12 def testCheckStrictModeReturnsNoErrorsWhenAllScriptElementsAreStrict(self): 13 contents = """ 14 <script> 'use strict'; var a = 1 + 1; 15 </script> 16 <br> 17 <script> 18 'use strict'; 19 var b = 2 + 2; 20 </script> 21 """ 22 self.assertEqual( 23 [], js_checks.CheckStrictMode(contents, is_html_file=True)) 24 25 def testCheckStrictModeReturnsNoErrorsWhenThereAreNoScriptTags(self): 26 contents = """ 27 <div></div> 28 """ 29 self.assertEqual( 30 [], js_checks.CheckStrictMode(contents, is_html_file=True)) 31 32 def testCheckStrictModeReturnsNoErrorsWhenJSFileIsStrict(self): 33 contents = """ 34 'use strict'; 35 var a = 1 + 1; 36 var b = 2 + 2; 37 """ 38 self.assertEqual( 39 [], js_checks.CheckStrictMode(contents, is_html_file=False)) 40 41 def testCheckStrictModeWhenThereIsACommentAboveTheDeclaration(self): 42 contents = """ 43 // This is a comment at the top 44 /* another comment which 45 spans two lines */ 46 'use strict'; 47 var a = 1 + 1; 48 var b = 2 + 2; 49 """ 50 self.assertEqual( 51 [], js_checks.CheckStrictMode(contents, is_html_file=False)) 52 53 def testCheckStrictModeDoesntCheckExternalScriptElements(self): 54 contents = """ 55 <script src="external.js"></script> 56 """ 57 self.assertEqual( 58 [], js_checks.CheckStrictMode(contents, is_html_file=True)) 59 60 def testCheckStrictModeReturnsAnErrorWhenOneScriptElementIsNotStrict(self): 61 contents = """ 62 <script> 'use strict'; var a = 1 + 1; 63 </script> 64 <br> 65 <script> 66 var b = 2 + 2; 67 </script> 68 """ 69 self.assertEqual( 70 1, len(js_checks.CheckStrictMode(contents, is_html_file=True))) 71 72 def testCheckStrictModeReturnsAnErrorWhenJSFileIsNonStrict(self): 73 contents = """ 74 var a = 1 + 1; 75 var b = 2 + 2; 76 """ 77 self.assertEqual( 78 1, len(js_checks.CheckStrictMode(contents, is_html_file=False))) 79