|
| 1 | +/* |
| 2 | + * Copyright © Magento, Inc. All rights reserved. |
| 3 | + * See COPYING.txt for license details. |
| 4 | + */ |
| 5 | + |
| 6 | +package com.magento.idea.magento2plugin.completion; |
| 7 | + |
| 8 | +import com.magento.idea.magento2plugin.BaseProjectTestCase; |
| 9 | +import com.magento.idea.magento2plugin.magento.packages.File; |
| 10 | +import java.util.Arrays; |
| 11 | +import java.util.List; |
| 12 | + |
| 13 | +abstract public class BaseCompletionTestCase extends BaseProjectTestCase { |
| 14 | + private static final String MESSAGE_NO_LOOKUP = "No lookup element was provided"; |
| 15 | + private final String testDataFolderPath |
| 16 | + = "testData" + File.separator + "completion" + File.separator; |
| 17 | + |
| 18 | + @Override |
| 19 | + protected void setUp() throws Exception { |
| 20 | + super.setUp(); |
| 21 | + myFixture.setTestDataPath(testDataFolderPath); |
| 22 | + } |
| 23 | + |
| 24 | + private void configureFixture(final String filePath) { |
| 25 | + myFixture.configureByFile(filePath); |
| 26 | + myFixture.completeBasic(); |
| 27 | + } |
| 28 | + |
| 29 | + public void assertCompletionContains(final String filePath, final String... lookupStrings) { |
| 30 | + configureFixture(filePath); |
| 31 | + |
| 32 | + final String messageEmptyLookup = "Failed that completion contains `%s`"; |
| 33 | + final String messageComplationContains = "Failed that completion contains `%s` in `%s`"; |
| 34 | + |
| 35 | + checkContainsCompletion(lookupStrings, messageEmptyLookup, messageComplationContains); |
| 36 | + } |
| 37 | + |
| 38 | + protected void assertFileContainsCompletions( |
| 39 | + final String filePath, |
| 40 | + final String... lookupStrings |
| 41 | + ) { |
| 42 | + configureFixture(filePath); |
| 43 | + |
| 44 | + final String messageEmptyLookup |
| 45 | + = "Failed that completion contains `%s` for file " + filePath; |
| 46 | + final String messageCompletionContains |
| 47 | + = "Failed that completion contains `%s` in `%s` for file " + filePath; |
| 48 | + |
| 49 | + checkContainsCompletion(lookupStrings, messageEmptyLookup, messageCompletionContains); |
| 50 | + } |
| 51 | + |
| 52 | + protected void assertFileNotContainsCompletions( |
| 53 | + final String filePath, |
| 54 | + final String... lookupStrings |
| 55 | + ) { |
| 56 | + configureFixture(filePath); |
| 57 | + |
| 58 | + final String messageEmptyLookup |
| 59 | + = "Failed that completion does not contain `%s` for file " + filePath; |
| 60 | + final String messageCompletionNotContains |
| 61 | + = "Failed that completion does not contain `%s` in `%s` for file " + filePath; |
| 62 | + |
| 63 | + checkDoesNotContainCompletion( |
| 64 | + lookupStrings, messageEmptyLookup, messageCompletionNotContains |
| 65 | + ); |
| 66 | + } |
| 67 | + |
| 68 | + protected void assertCompletionNotShowing(final String filePath) { |
| 69 | + configureFixture(filePath); |
| 70 | + |
| 71 | + final List<String> lookupElements = myFixture.getLookupElementStrings(); |
| 72 | + |
| 73 | + if (lookupElements != null && !lookupElements.isEmpty()) { |
| 74 | + final String messageCompletionDoesNotShow |
| 75 | + = "Failed asserting that completion does not show up"; |
| 76 | + |
| 77 | + fail(messageCompletionDoesNotShow); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + protected void checkContainsCompletion( |
| 82 | + final String[] lookupStrings, |
| 83 | + final String emptyLookupError, |
| 84 | + final String completionContainsError |
| 85 | + ) { |
| 86 | + if (lookupStrings.length == 0) { |
| 87 | + fail(MESSAGE_NO_LOOKUP); |
| 88 | + } |
| 89 | + |
| 90 | + final List<String> lookupElements = myFixture.getLookupElementStrings(); |
| 91 | + |
| 92 | + if (lookupElements == null || lookupElements.isEmpty()) { |
| 93 | + fail(String.format(emptyLookupError, Arrays.toString(lookupStrings))); |
| 94 | + } |
| 95 | + |
| 96 | + for (final String lookupString : lookupStrings) { |
| 97 | + if (!lookupElements.contains(lookupString)) { |
| 98 | + fail(String.format( |
| 99 | + completionContainsError, lookupString, lookupElements.toString()) |
| 100 | + ); |
| 101 | + } |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + protected void checkDoesNotContainCompletion( |
| 106 | + final String[] lookupStrings, |
| 107 | + final String emptyLookupError, |
| 108 | + final String completionDoesNotContainError |
| 109 | + ) { |
| 110 | + if (lookupStrings.length == 0) { |
| 111 | + fail(MESSAGE_NO_LOOKUP); |
| 112 | + } |
| 113 | + |
| 114 | + final List<String> lookupElements = myFixture.getLookupElementStrings(); |
| 115 | + |
| 116 | + if (lookupElements == null || lookupElements.isEmpty()) { |
| 117 | + fail(String.format(emptyLookupError, Arrays.toString(lookupStrings))); |
| 118 | + } |
| 119 | + |
| 120 | + for (final String lookupString : lookupStrings) { |
| 121 | + if (lookupElements.contains(lookupString)) { |
| 122 | + fail(String.format( |
| 123 | + completionDoesNotContainError, lookupString, lookupElements.toString()) |
| 124 | + ); |
| 125 | + } |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + protected abstract String getFixturePath(String fileName); |
| 130 | +} |
0 commit comments