• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python
2
3#
4# Copyright (C) 2024 The Android Open Source Project
5#
6# Licensed under the Apache License, Version 2.0 (the "License");
7# you may not use this file except in compliance with the License.
8# You may obtain a copy of the License at
9#
10#      http://www.apache.org/licenses/LICENSE-2.0
11#
12# Unless required by applicable law or agreed to in writing, software
13# distributed under the License is distributed on an "AS IS" BASIS,
14# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15# See the License for the specific language governing permissions and
16# limitations under the License.
17#
18
19import functools
20import sys
21import unittest
22
23import commandline
24
25
26class CommandlineTest(unittest.TestCase):
27
28  def fileread(filemap, path):
29    return filemap[path]
30
31  def test_commandline(self):
32    filemap = {}
33    filemap["aliases.json"] = (
34        """[{"name": "sans-serif-thin", "to": "sans-serif", "weight": 100}]"""
35    )
36    filemap["fallbacks.json"] = (
37        """[{"lang": "und-Arab"},{"lang": "und-Ethi"}]"""
38    )
39    filemap["family.json"] = """[{
40      "name": "sans-serif",
41      "fonts": [{
42        "file": "Roboto-Regular.ttf",
43        "supportedAxes": "wght,ital",
44        "axes": { "wdth": "100" }
45      }]
46    }, {
47      "name": "sans-serif-condensed",
48      "fonts": [{
49        "file": "Roboto-Regular.ttf",
50        "supportedAxes": "wght,ital",
51        "axes": { "wdth": "75" }
52      }]
53    }]"""
54
55    filemap["family2.json"] = """[{
56      "name": "roboto-flex",
57      "fonts": [{
58        "file": "RobotoFlex-Regular.ttf",
59        "supportedAxes": "wght",
60        "axes": { "wdth": "100" }
61      }]
62    }]"""
63
64    args = commandline.parse_commandline(
65        [
66            "-o",
67            "output.xml",
68            "--alias",
69            "aliases.json",
70            "--fallback",
71            "fallbacks.json",
72            "family.json",
73            "family2.json",
74        ],
75        functools.partial(CommandlineTest.fileread, filemap),
76    )
77
78    self.assertEqual("output.xml", args.outfile)
79
80    self.assertEqual(1, len(args.aliases))
81    self.assertEqual("sans-serif-thin", args.aliases[0].name)
82    self.assertEqual("sans-serif", args.aliases[0].to)
83    self.assertEqual(100, args.aliases[0].weight)
84
85    self.assertEqual(2, len(args.fallback))
86    # Order is not a part of expectation. Check the expected lang is included.
87    langs = set(["und-Arab", "und-Ethi"])
88    self.assertTrue(args.fallback[0].lang in langs)
89    self.assertTrue(args.fallback[1].lang in langs)
90
91    self.assertEqual(3, len(args.families))
92    # Order is not a part of expectation. Check the expected name is included.
93    names = set(["sans-serif", "sans-serif-condensed", "roboto-flex"])
94    self.assertTrue(args.families[0].name in names)
95    self.assertTrue(args.families[1].name in names)
96    self.assertTrue(args.families[2].name in names)
97
98
99if __name__ == "__main__":
100  unittest.main(verbosity=2)
101