• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python
2# Copyright (c) 2014 The Chromium OS 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
7"""Unittest for ini_editor.py."""
8
9
10import logging
11import os
12import re
13import tempfile
14import unittest
15
16from ini_editor import Ini
17
18SAMPLE_INI="""\
19[output_source]
20library=builtin
21label=source
22purpose=playback
23disable=(equal? output_jack "HDMI")
24output_0={src:0}
25output_1={src:1}
26
27[output_sink]
28library=builtin
29label=sink
30purpose=playback
31disable=(equal? output_jack "HDMI")
32input_0={dst:0}
33input_1={dst:1}
34
35[drc]
36library=builtin
37label=drc
38input_0={src:0}
39input_1={src:1}
40output_2={intermediate:0}
41output_3={intermediate:1}
42input_4=0       ; f
43input_5=0       ; enable
44input_6=-29     ; threshold
45input_7=3       ; knee
46input_8=6.677   ; ratio
47input_9=0.02    ; attack
48input_10=0.2     ; release
49input_11=-7      ; boost
50
51[eq2]
52library=builtin
53label=eq2
54input_0={intermediate:0}
55input_1={intermediate:1}
56output_2={dst:0}
57output_3={dst:1}
58input_4=6       ; peaking
59input_5=380     ; freq
60input_6=3       ; Q
61input_7=-10     ; gain
62input_8=6       ; peaking
63input_9=450     ; freq
64input_10=3       ; Q
65input_11=-12     ; gain
66"""
67
68SAMPLE_INI_DRC="""\
69[drc]
70library=builtin
71label=drc
72input_0={src:0}
73input_1={src:1}
74output_2={intermediate:0}
75output_3={intermediate:1}
76input_4=0       ; f
77input_5=0       ; enable
78input_6=-29     ; threshold
79input_7=3       ; knee
80input_8=6.677   ; ratio
81input_9=0.02    ; attack
82input_10=0.2     ; release
83input_11=-7      ; boost
84
85"""
86
87SAMPLE_INI_DRC_INSERTED="""\
88[drc]
89library=builtin
90label=drc
91input_0={src:0}
92input_1={src:1}
93output_2={intermediate:0}
94output_3={intermediate:1}
95input_4=1       ; new_parameter
96input_5=0       ; f
97input_6=0       ; enable
98input_7=-29     ; threshold
99input_8=3       ; knee
100input_9=6.677   ; ratio
101input_10=0.02    ; attack
102input_11=0.2     ; release
103input_12=-7      ; boost
104
105"""
106
107
108class IniTest(unittest.TestCase):
109  """Unittest for Ini class."""
110  def setUp(self):
111    self.ini_file = tempfile.NamedTemporaryFile(prefix='ini_editor_unittest')
112    with open(self.ini_file.name, 'w') as f:
113      f.write(SAMPLE_INI)
114    self.ini = Ini(self.ini_file.name)
115
116  def tearDown(self):
117    self.ini_file.close()
118
119  def testPrint(self):
120    """Unittest for Print method of Ini class."""
121    output = self.ini.Print()
122    self.assertEqual(output.getvalue(), SAMPLE_INI)
123
124  def testHasSection(self):
125    """Unittest for HasSection method of Ini class."""
126    self.assertTrue(self.ini.HasSection('drc'))
127    self.assertFalse(self.ini.HasSection('eq1'))
128
129  def testPrintSection(self):
130    """Unittest for PrintSection method of Ini class."""
131    output = self.ini.PrintSection('drc')
132    self.assertEqual(output.getvalue(), SAMPLE_INI_DRC)
133
134  def testInsertLineToSection(self):
135    """Unittest for InsertLineToSection method of Ini class."""
136    self.ini.InsertLineToSection('drc', 'input_4=1       ; new_parameter')
137    output = self.ini.PrintSection('drc')
138    self.assertEqual(output.getvalue(), SAMPLE_INI_DRC_INSERTED)
139
140
141if __name__ == '__main__':
142  logging.basicConfig(
143     format='%(asctime)s:%(levelname)s:%(filename)s:%(lineno)d:%(message)s',
144     level=logging.DEBUG)
145  unittest.main()
146