• Orgad Shaneh's avatar
    CppTools: Make completion tests data-driven · 0909c029
    Orgad Shaneh authored
    Mostly done using the following Ruby script:
    
    Fname = 'src/plugins/cpptools/cppcompletion_test.cpp'
    s = File.read(Fname)
    mod = s.gsub(/(?<declaration>void CppToolsPlugin::test_(?<test_name>[^(]+)\(\)\n\{)(?<body>.*?\n)\}/m) { |func|
      match = $~
      declaration = match['declaration']
      body = match['body']
      test_name = match['test_name'].sub('_data', '')
      final = body.gsub(/(?:completions.clear\(\);\s+)?(?: +)[^\n]* =\n(?<code>.*?);\s*(?<completions>(?:completions.append\(QLatin1String\("[^"]*"\)\);\s*)*)QTest::newRow\("case: (?<name>.+?)"\)\s*<< code << _\("(?<prefix>[^"]+)"\) << completions;/m) {
        m = $~
        res = "    QTest::newRow(\"#{test_name}: #{m['name']}\") << _(\n#{m['code'].rstrip}\n        ) << _(\"#{m['prefix']}\") << (QStringList()"
        m['completions'].scan(/completions.append\((.+)\);/) { |comp|
          res << "\n            << #{comp[0]}"
        }
        res + ');'
      }.gsub(/(?: +)[^\n]* =\n(?<code>.*?);\n\s*CompletionTestCase test\(.+?, "(?<prefix>.+?)"\);\s*QStringList expected;\s*(?<completions>(?:expected.append\(QLatin1String\("[^"]*"\)\);\s*)*)const QStringList completions = test.getCompletions\(\);\s*QCOMPARE\(completions, expected\);/m) {
        m = $~
        res = "    QTest::newRow(\"#{test_name}\") << _(\n#{m['code'].rstrip}\n        ) << _(\"#{m['prefix']}\") << (QStringList()"
        m['completions'].scan(/expected.append\((.+)\);/) { |comp|
          res << "\n            << #{comp[0]}"
        }
        res + ');'
      }.gsub(/(?: +)[^\n]* =\n(?<code>.*?);\n\s*CompletionTestCase test\(.+?(?:, (?<prefix>".+?"))?\);\s*(?:const )?QStringList completions = test.getCompletions\(\);\s*QCOMPARE\(completions.size\(\), \d+\);(?<completions>(?:\s*QVERIFY\(completions.contains\([^\n]+\);)*)\n/m) { |mm|
        m = $~
        res = "    QTest::newRow(\"#{test_name}\") << _(\n#{m['code'].rstrip}\n        ) << _(#{m['prefix']}) << (QStringList()"
        m['completions'].scan(/QVERIFY\(completions.contains\((.+?)\)\);/) { |comp|
          res << "\n            << #{comp[0]}"
        }
        res + ");\n"
      }.gsub(/(?: +)[^\n]* =\n(?<code>.*?);\n\s*CompletionTestCase test\(.+?(?:, (?<prefix>".+?"))?\);\s*bool replaceAccessOperator = false;\s*const QStringList completions = test.getCompletions\(\&replaceAccessOperator\);\s*QCOMPARE\(completions.size\(\), \d+\);(?<completions>(?:\s*QVERIFY\(completions.contains\([^\n]+\);)*)\s*QVERIFY\((?<replace>!?)[^)]*\);\n/m) { |mm|
        m = $~
        res = "    QTest::newRow(\"#{test_name}\") << _(\n#{m['code'].rstrip}\n        ) << _(#{m['prefix']}) << (QStringList()"
        m['completions'].scan(/QVERIFY\(completions.contains\((.+?)\)\);/) { |comp|
          res << "\n            << #{comp[0]}"
        }
        res + ")\n        << #{m['replace'].empty?};\n"
      }
      if final == body or final['QTest::addColumn']
        declaration + final + "}"
      else
        final
      end
    }.gsub(/QTest::newRow\("([^"]+)"/) { |m|
      name = $1
      if name.size > 73
        space = name[0..73].rindex(/[ _]/)
        "QTest::newRow(\"#{name[0..space]}\"\n                  \"#{name[space+1..-1]}\""
      else
        m
      end
    }.gsub(/\s+QTest::newRow/, "\n\n    QTest::newRow")
    if mod != s
        File.open(Fname, 'wt').write(mod)
    end
    
    Change-Id: Id6bfb03cdf31ac27b36028fcdc861c340a5398f4
    Reviewed-by: default avatarNikolai Kosjar <nikolai.kosjar@digia.com>
    0909c029
cpptoolsplugin.h 5.8 KB