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
14module Generator
15  class Test
16    def initialize(test, predefined, process_only, output, skip_header)
17      # test - YAML:
18      #   name: Generator::TEST_NAME
19      #   instruction: Generator::TEST_INSTRUCTION
20      #   command: Generator::TEST_COMMANDS
21      @test = test
22      @predefined = predefined
23
24      @test_name = test[Generator::TEST_NAME]
25      @isa = test[Generator::TEST_ISA]
26      @commands = test[Generator::TEST_COMMANDS]
27      @skip = @test[Generator::TEST_SKIP]
28      @process_only = process_only
29      @output = output
30      @definitions = Definitions.new @test[Generator::DEFINITIONS]
31      @skip_header = skip_header
32    end
33
34    def parse
35      LOG.info "START parsing test '#{@test_name}'"
36
37      if !@skip
38        if @test.key?(Generator::TEST_COMMANDS)
39          parse_commands
40        else
41          LOG.error "Test '#{@test_name}' does not have definition of instruction commands"
42        end
43      else
44        LOG.warn "Skip test '#{@test_name}' generation due to skip property is set"
45      end
46    end
47
48    def parse_commands
49      @commands.each do |raw_command|
50        if @process_only
51          process = raw_command.key?(Generator::TEST_COMMAND_ONLY) && raw_command[Generator::TEST_COMMAND_ONLY]
52          LOG.debug "Some command has 'only' key, process command: #{process}"
53        else
54          process = true
55          LOG.debug "No 'only' key is defined for any command, process command: #{process}"
56        end
57        if process
58          command = Command.new raw_command, @isa, @test_name, @definitions, @predefined, @output, @skip_header
59          command.parse
60        end
61
62      end
63    end
64  end
65end
66