1#!/pxrpythonsubst 2# 3# Copyright 2016 Pixar 4# 5# Licensed under the Apache License, Version 2.0 (the "Apache License") 6# with the following modification; you may not use this file except in 7# compliance with the Apache License and the following modification to it: 8# Section 6. Trademarks. is deleted and replaced with: 9# 10# 6. Trademarks. This License does not grant permission to use the trade 11# names, trademarks, service marks, or product names of the Licensor 12# and its affiliates, except as required to comply with Section 4(c) of 13# the License and to reproduce the content of the NOTICE file. 14# 15# You may obtain a copy of the Apache License at 16# 17# http://www.apache.org/licenses/LICENSE-2.0 18# 19# Unless required by applicable law or agreed to in writing, software 20# distributed under the Apache License with the above modification is 21# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 22# KIND, either express or implied. See the Apache License for the specific 23# language governing permissions and limitations under the Apache License. 24# 25 26from __future__ import print_function 27 28from pxr import Tf 29import unittest 30 31def Replace(tmpl, safe=False, **kw): 32 ts = Tf.TemplateString(tmpl) 33 result = '' 34 if safe: 35 result = ts.SafeSubstitute(kw) 36 else: 37 result = ts.Substitute(kw) 38 print(repr(tmpl), ' -> ', repr(result)) 39 return result 40 41class TestTfTemplateString(unittest.TestCase): 42 43 def test_TemplateString(self): 44 self.assertTrue(Tf.TemplateString().valid) 45 self.assertTrue(Tf.TemplateString('').valid) 46 self.assertFalse(Tf.TemplateString('${}').valid) 47 self.assertTrue(Tf.TemplateString('$').valid) 48 self.assertTrue(Tf.TemplateString('$$').valid) 49 50 self.assertFalse(Tf.TemplateString('${} ${} ${} ').valid) 51 self.assertEqual(3, len(Tf.TemplateString('${} ${} ${} ').GetParseErrors())) 52 53 self.assertFalse(Tf.TemplateString('${').valid) 54 self.assertTrue(Tf.TemplateString('${').GetParseErrors()) 55 56 t = Tf.TemplateString('${foo}') 57 self.assertEqual('${foo}', t.template) 58 self.assertTrue(t.valid) 59 self.assertFalse(t.GetParseErrors()) 60 61 def test_Substitute(self): 62 self.assertEqual('value', Replace('$var', var='value')) 63 self.assertEqual('valued', Replace('${var}d', var='value')) 64 self.assertEqual('value-value', Replace('$var-value', var='value')) 65 self.assertEqual('0000', Replace('$var$var$var$var', var='0')) 66 self.assertEqual('0.0.0.0', Replace('${var}.${var}.${var}.${var}', var='0')) 67 68 self.assertEqual("//brave/b952/shot/b952_17/b952_17.menva", 69 Replace("//$unit/$prod/shot/$shot/$shot.menva", 70 unit='brave', prod='b952', shot='b952_17')) 71 72 self.assertEqual("Please remit the $sum of $19.95", 73 Replace("Please remit the $$sum of $$$sum", sum='19.95')) 74 75 Replace("Unreplaced placeholders ${are} awesome", safe=True) 76 77 with self.assertRaises(Tf.ErrorException): 78 Replace("Unreplaced placeholders ${are} awesome") 79 80 with self.assertRaises(Tf.ErrorException): 81 Replace("Invalid characters in placeholders ${are not awesome") 82 83 with self.assertRaises(Tf.ErrorException): 84 Replace("Never stop ${quoting") 85 86 with self.assertRaises(Tf.ErrorException): 87 Replace("${}") 88 89 with self.assertRaises(Tf.ErrorException): 90 Replace("${ }") 91 92 93 def test_EmptyMapping(self): 94 t = Tf.TemplateString("//$unit/$prod/shot/$shot/$shot.menva") 95 m = t.GetEmptyMapping() 96 self.assertTrue('unit' in m) 97 self.assertTrue('prod' in m) 98 self.assertTrue('shot' in m) 99 100 101 t = Tf.TemplateString("${ }") 102 m = t.GetEmptyMapping() 103 self.assertEqual(0, len(m)) 104 self.assertFalse(t.valid) 105 self.assertNotEqual(0, len(t.GetParseErrors())) 106 107if __name__ == '__main__': 108 unittest.main() 109 110