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 14# frozen_string_literal: true 15 16require 'stringio' 17 18require_relative 'test_base' 19 20module Generator 21 class TestCase < TestBase 22 def initialize(command, current_case, instruction, definitions, predefined, template, skip_header) 23 super instruction, command, definitions, predefined, skip_header 24 # TODO: improve this logging to avoid all hashes in the log 25 LOG.debug "TestCase created with '#{command}' '#{current_case}' '#{instruction}'" 26 27 # command - YAML: 28 # sig: Generator::TEST__SIG 29 # check-type: Generator::CASE_CHECK_TYPE 30 # cases: Generator::CASES 31 # 32 # current_case - YAML: 33 # values: Generator::CASE_VALUES 34 # case-check-type: Generator::CASE_CHECK_TYPE 35 # code-template: Generator::CASE_TEMPLATE 36 37 @command = command 38 @current_case = current_case 39 @instruction = instruction 40 @definitions = definitions 41 @predefined = predefined 42 @template = template 43 end 44 45 46 def get_header_template() 47 headers = super() 48 case_headers = @current_case[Generator::CASE_HEADER_TEMPLATE] 49 if headers.nil? && case_headers.nil? 50 nil 51 else 52 (case_headers||[]).concat(headers || []) 53 end 54 end 55 56 57 def create_single_test_case(bugids, ignore, tags) 58 StringIO.open do |content| 59 write_test_initial_block content 60 61 test_run_options = @command[Generator::TEST_RUN_OPTIONS] 62 test_description = @command[Generator::TEST_DESCRIPTION] || "" 63 case_run_options = @current_case[Generator::CASE_RUN_OPTIONS] 64 run_options = case_run_options unless case_run_options.nil? 65 run_options = test_run_options if run_options.nil? && !test_run_options.nil? 66 run_options = [] if run_options.nil? 67 68 ignore_test = @command[Generator::TEST_IGNORE] || false 69 bugids_test = @command[Generator::TEST_BUG_ID] || [] 70 tags_test = @command[Generator::TEST_TAGS] || [] 71 ignore_case = @current_case.fetch(Generator::CASE_IGNORE, ignore_test || ignore) 72 bugids_case = @current_case[Generator::CASE_BUG_ID] || [] 73 tags_case = @current_case[Generator::CASE_TAGS] || [] 74 description = @current_case[Generator::CASE_DESCRIPTION] || "" 75 test_panda_options = @command[Generator::TEST_PANDA_OPTIONS] || "" 76 77 write_runner_options content, run_options, ignore_case, bugids + bugids_test + bugids_case, tags_test + tags + tags_case, 78 test_description + " " + description, test_panda_options 79 80 write_test_main_block content 81 82 template = if @current_case.key?(Generator::CASE_TEMPLATE) 83 LOG.debug 'Case has own template, use it' 84 @current_case[Generator::CASE_TEMPLATE] 85 else 86 LOG.debug 'Use main template for current tests' 87 @template 88 end 89 90 if @current_case.key?(Generator::CASE_VALUES) 91 LOG.debug "substitute values: #{@current_case[Generator::CASE_VALUES]}" 92 93 begin 94 values = @current_case[Generator::CASE_VALUES].map do |val| 95 LOG.debug "Parse -#{val}-" 96 val.to_s.each_line.map do |single_line| 97 # string interpolation if needed 98 if single_line.match(/\#\{.+\}/) 99 LOG.debug "'#{single_line}' evaluate" 100 single_line = eval(%Q["#{single_line}"]) 101 end 102 if match = single_line.match(/(.+)##\*([[:digit:]]+)$/) 103 str, num = match.captures 104 LOG.debug "'#{str}'' repeat #{num} times" 105 "#{str}\n"*num.to_i 106 else 107 LOG.debug "simple value `#{single_line}`" 108 single_line 109 end 110 end.join 111 end 112 updated = format template, *values 113 114 # Remove lines marked to remove by ##- tag 115 updated = updated.to_s.each_line.map do |single_line| 116 if /(.*)##\-(.*)/ =~ single_line 117 "" 118 else 119 single_line 120 end 121 end.join 122 123 rescue StandardError => e 124 LOG.error 'Cannot substitute values to template' 125 LOG.error "Template: #{template}" 126 LOG.error "Values: #{@current_case[Generator::CASE_VALUES]}" 127 LOG.error "Instruction: #{@instruction}" 128 LOG.error "Command: #{@command}" 129 LOG.error e 130 raise 'Failed to create tests' 131 end 132 133 content.puts updated 134 else 135 LOG.debug 'No values to substitute' 136 content.puts template 137 end 138 139 test_check_type = @command.key?(Generator::TEST_CHECK_TYPE) ? @command[Generator::TEST_CHECK_TYPE] : Generator::TEMPLATE_CHECK_POSITIVE 140 case_check_type = @current_case.key?(Generator::CASE_CHECK_TYPE) ? @current_case[Generator::CASE_CHECK_TYPE] : test_check_type 141 142 # LOG.debug "case checking type is #{case_check_type}" 143 content.puts @predefined.definition case_check_type 144 145 write_test_main_wrapper_block content, run_options 146 147 content.string 148 end 149 end 150 end 151end 152