1# Copyright (c) 2021-2022 Huawei Device Co., Ltd.
2# Licensed under the Apache License, Version 2.0 (the "License");
3# you may not use this file except in compliance with the License.
4# You may obtain a copy of the License at
5#
6# http://www.apache.org/licenses/LICENSE-2.0
7#
8# Unless required by applicable law or agreed to in writing, software
9# distributed under the License is distributed on an "AS IS" BASIS,
10# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11# See the License for the specific language governing permissions and
12# limitations under the License.
13
14require_relative './single_test'
15
16module Generator
17  class Command
18    def initialize(command, isa, test_name, definitions, predefined, output, skip_header)
19
20      # command - YAML:
21      #   isa: Generator::TEST_ISA
22      #       title: Generator::TEST_ISA_TITLE
23      #       description: Generator::TEST_ISA_DESCRIPTION
24      #   file-name:
25      #   skip: Generator::TEST_COMMAND_SKIP
26      #   code-template: Generator::TEST_CODE_TEMPLATE
27      #   cases: Generator::CASES
28
29      @command = command
30      @isa = isa
31      @test_name = test_name
32      @predefined = predefined
33      @definitions = definitions
34      @output = output
35      @skip_header = skip_header
36      LOG.debug 'Command:initialize'
37      LOG.debug command
38
39      @skip = command.key?(Generator::TEST_COMMAND_SKIP) ? command[Generator::TEST_COMMAND_SKIP] : false
40
41    end
42
43    def parse()
44      if @skip
45        LOG.info("Skip test '#{@test_name}' due to \"skip\": true")
46        return
47      end
48      if @command.key?(Generator::CASES)
49        LOG.debug "Test '#{@test_name}' contains several cases"
50        create_test_cases
51      else
52        LOG.debug "Test '#{@test_name}' has no cases"
53        create_single_test
54      end
55    end
56
57    def create_test_cases
58      sig_name = @command[Generator::COMMAND_FILE_NAME]
59
60      test_dir = "#{@output}/#{@test_name}"
61
62      FileUtils.mkdir_p test_dir unless File.exist? test_dir
63
64      if @command.key?(Generator::TEST_TEMPLATE_CASES)
65        LOG.debug "#{@test_name} has template cases"
66        @command[Generator::TEST_TEMPLATE_CASES].each_with_index do |template_case, idx1|
67          values = template_case[Generator::TEST_TEMPLATE_CASE_VALUES]
68          excludes = template_case[Generator::TEST_TEMPLATE_CASE_EXCLUDE_VAL] || []
69          bugids = template_case[Generator::TEST_TEMPLATE_CASE_BUGID] || []
70          ignore = template_case[Generator::TEST_TEMPLATE_CASE_IGNORE] || false
71          tags = template_case[Generator::TEST_TEMPLATE_CASE_TAG] || []
72
73          template = format @command[Generator::TEST_CODE_TEMPLATE], *values
74          template = template.gsub('*s', '%s')
75          index = 0
76          @command[Generator::CASES].each do |current_case|
77            if !excludes.include? current_case[Generator::CASE_ID]
78              LOG.debug "Process case '#{@test_name}_#{sig_name}_#{idx1}_#{index}'"
79              file_name = "#{test_dir}/#{@test_name}_#{sig_name}_#{idx1}_#{index}.pa"
80              process_case_values file_name, current_case, index, sig_name, test_dir, template, bugids, ignore, tags
81              index = index + 1
82            end
83          end
84        end
85      else
86        @command[Generator::CASES].each_with_index do |current_case, index|
87          LOG.debug "Process case '#{@test_name}_#{sig_name}_#{index}'"
88          file_name = "#{test_dir}/#{@test_name}_#{sig_name}_#{index}.pa"
89          template = @command[Generator::TEST_CODE_TEMPLATE]
90          process_case_values file_name, current_case, index, sig_name, test_dir, template, [], false, []
91        end
92      end
93
94
95    end
96
97    def process_case_values(file_name, current_case, index, sig_name, test_dir, template, bugids, ignore, tags)
98
99      File.open(file_name, 'w') do |test_file|
100        test_case = TestCase.new @command, current_case, @isa, @definitions, @predefined, template, @skip_header
101        output = test_case.create_single_test_case bugids, ignore, tags
102        test_file.puts output
103      end
104    end
105
106    def create_single_test
107      test_dir = "#{@output}/#{@test_name}"
108      FileUtils.mkdir_p test_dir unless File.exist? test_dir
109      file_name = @command[Generator::COMMAND_FILE_NAME]
110
111      File.open("#{test_dir}/#{@test_name}_#{file_name}.pa", 'w') do |test_file|
112        test = SingleTest.new @command, @isa, @definitions, @predefined, @skip_header
113        test_file.puts test.create_single_test
114        # test_file.puts @predefined.definition check_type
115      end
116    end
117  end
118end
119