<commit_before>// Copyright (c) 2012, the Dart project authors.  Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

import 'dart:io';
import 'package:unittest/unittest.dart';
import 'package:doc_code_verify/doc_code_verifier.dart';

Directory get scriptDir => new File(new Options().script).directorySync();
Directory get projectDir => new Directory(new Path(scriptDir.path).append("..").canonicalize().toNativePath());
Directory get sourceDir => new Directory(new Path(scriptDir.path).append("/sourceDir").canonicalize().toNativePath());
Directory get documentationDir => new Directory(new Path(scriptDir.path).append("/documentationDir").canonicalize().toNativePath());

/**
 * Call a callback with a temporary directory.
 *
 * Delete it once the callback is done.
 */
void callWithTemporaryDirectorySync(void callback(Directory temp)) {
  Directory temp = new Directory("").createTempSync();
  try {
    callback(temp);
  } finally {
    if (temp.existsSync()) {
      temp.deleteSync(recursive: true);
    }
  }
}

void main() {
  group('DocCodeVerifier', () {
    DocCodeVerifier verifier;

    setUp(() {
      verifier = new DocCodeVerifier();
    });

    test("scriptName should be doc_code_verify.dart", () {
      expect(verifier.scriptName, equals("doc_code_verify.dart"));
    });

    test("projectDir should have a bin directory in it", () {
      expect(new Directory(new Path(projectDir.path).append("bin").toNativePath()).existsSync(),
             isTrue);
    });

    test('the syntax for example names is pretty permissive', () {
      expect(DocCodeVerifier.beginRegExp.firstMatch("BEGIN(a b/c)"), isNotNull);
      expect(DocCodeVerifier.verifyBlockRegExp.firstMatch("VERIFY(a b/c)"), isNotNull);
      expect(DocCodeVerifier.endRegExp.firstMatch("END(a b/c)"), isNotNull);
    });

    test('correctly matches inline verifys', () {
      expect(DocCodeVerifier.inlineVerifyRegExp.firstMatch("(VERIFY(example))"), isNotNull);
    });

    test('examples is empty by default', () {
      expect(verifier.examples.isEmpty, isTrue);
    });

    test('scanForExamples updates examples', () {
      verifier.scanForExamples("""
        // BEGIN(add)
        num add(num a, num b) {
          return a + b;
        }
        // END(add)

        void main() {
          print("Hello, World!");
        }
      """);

      expect(verifier.examples["add"].join(), equalsIgnoringWhitespace("""
        num add(num a, num b) {
          return a + b;
        }
      """));
    });

    test('scanForExamples permits overlapping examples', () {
      verifier.scanForExamples("""
        // BEGIN(two_lines)
        line
        // BEGIN(one_line)
        line
        // END(two_lines)
        // END(one_line)
        line
        line
      """);

      expect(verifier.examples["one_line"].join(), equalsIgnoringWhitespace("""
        line
      """));

      expect(verifier.examples["two_lines"].join(), equalsIgnoringWhitespace("""
        line
        line
      """));
    });
    
    //Each example can only have one block in the source.
    test('scanForExamples does not concatenate examples with the same name in a file', () {
      var printedError = false;
      
      void _print(String s) {
        expect(s, equals("doc_code_verify.dart: Warning, the name `example` was already used"));
        printedError = true;
      }
        
      verifier.scanForExamples("""
        // BEGIN(example)
        line
        // END(example)
        line
        line
        // BEGIN(example)
        line
        // END(example)
      """, print: _print);

      expect(verifier.examples["example"].join(), equalsIgnoringWhitespace("""
        line
      """));
      
      expect(verifier.errorsEncountered, isTrue);
    });
    
    test('scanForExamples complains if an example was added in another file', () {
      var printedError = false;
      
      void _print(String s) {
        expect(s, equals("doc_code_verify.dart: Warning, the name `example` was already used"));
        printedError = true;
      }
        
      verifier.scanForExamples("""
        // BEGIN(example)
        line
        // END(example)
      """, print: _print);
      verifier.scanForExamples("""<commit_msg>Made test more explicit<commit_after>// Copyright (c) 2012, the Dart project authors.  Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

import 'dart:io';
import 'package:unittest/unittest.dart';
import 'package:doc_code_verify/doc_code_verifier.dart';

Directory get scriptDir => new File(new Options().script).directorySync();
Directory get projectDir => new Directory(new Path(scriptDir.path).append("..").canonicalize().toNativePath());
Directory get sourceDir => new Directory(new Path(scriptDir.path).append("/sourceDir").canonicalize().toNativePath());
Directory get documentationDir => new Directory(new Path(scriptDir.path).append("/documentationDir").canonicalize().toNativePath());

/**
 * Call a callback with a temporary directory.
 *
 * Delete it once the callback is done.
 */
void callWithTemporaryDirectorySync(void callback(Directory temp)) {
  Directory temp = new Directory("").createTempSync();
  try {
    callback(temp);
  } finally {
    if (temp.existsSync()) {
      temp.deleteSync(recursive: true);
    }
  }
}

void main() {
  group('DocCodeVerifier', () {
    DocCodeVerifier verifier;

    setUp(() {
      verifier = new DocCodeVerifier();
    });

    test("scriptName should be doc_code_verify.dart", () {
      expect(verifier.scriptName, equals("doc_code_verify.dart"));
    });

    test("projectDir should have a bin directory in it", () {
      expect(new Directory(new Path(projectDir.path).append("bin").toNativePath()).existsSync(),
             isTrue);
    });

    test('the syntax for example names is pretty permissive', () {
      expect(DocCodeVerifier.beginRegExp.firstMatch("BEGIN(a b/c)"), isNotNull);
      expect(DocCodeVerifier.verifyBlockRegExp.firstMatch("VERIFY(a b/c)"), isNotNull);
      expect(DocCodeVerifier.endRegExp.firstMatch("END(a b/c)"), isNotNull);
    });

    test('correctly matches inline verifys', () {
      expect(DocCodeVerifier.inlineVerifyRegExp.firstMatch("(VERIFY(example))"), isNotNull);
    });

    test('examples is empty by default', () {
      expect(verifier.examples.isEmpty, isTrue);
    });

    test('scanForExamples updates examples', () {
      verifier.scanForExamples("""
        // BEGIN(add)
        num add(num a, num b) {
          return a + b;
        }
        // END(add)

        void main() {
          print("Hello, World!");
        }
      """);

      expect(verifier.examples["add"].join(), equalsIgnoringWhitespace("""
        num add(num a, num b) {
          return a + b;
        }
      """));
    });

    test('scanForExamples permits overlapping examples', () {
      verifier.scanForExamples("""
        // BEGIN(two_lines)
        line
        // BEGIN(one_line)
        line
        // END(two_lines)
        // END(one_line)
        line
        line
      """);

      expect(verifier.examples["one_line"].join(), equalsIgnoringWhitespace("""
        line
      """));

      expect(verifier.examples["two_lines"].join(), equalsIgnoringWhitespace("""
        line
        line
      """));
    });
    
    //Each example can only have one block in the source.
    test('scanForExamples does not concatenate examples with the same name in a file', () {
      var printedError = false;
      
      void _print(String s) {
        expect(s, equals("doc_code_verify.dart: Warning, the name `example` was already used"));
        printedError = true;
      }
        
      verifier.scanForExamples("""
        // BEGIN(example)
        line
        // END(example)
        line
        line
        // BEGIN(example)
        2 lines
        2 lines
        // END(example)
      """, print: _print);

      expect(verifier.examples["example"].join(), equalsIgnoringWhitespace("""
        line
      """));
      
      expect(verifier.errorsEncountered, isTrue);
    });
    
    test('scanForExamples complains if an example was added in another file', () {
      var printedError = false;
      
      void _print(String s) {
        expect(s, equals("doc_code_verify.dart: Warning, the name `example` was already used"));
        printedError = true;
      }
        
      verifier.scanForExamples("""
        // BEGIN(example)
        line
        // END(example)
      """, print: _print);
      verifier.scanForExamples("""
{'fraction_non_alphanumeric': 0.13775685914361152, 'fraction_numerical': 0.0011479738261967627, 'mean_word_length': 2.674398987768874, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4717682', 'n_tokens_mistral': 2576, 'n_tokens_neox': 2470, 'n_words': 585}