<commit_before>/*
 *  Licensed to the Apache Software Foundation (ASF) under one
 *  or more contributor license agreements.  See the NOTICE file
 *  distributed with this work for additional information
 *  regarding copyright ownership.  The ASF licenses this file
 *  to you under the Apache License, Version 2.0 (the
 *  "License"); you may not use this file except in compliance
 *  with the License.  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing,
 *  software distributed under the License is distributed on an
 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 *  KIND, either express or implied.  See the License for the
 *  specific language governing permissions and limitations
 *  under the License.
 */


package gdk

import groovy.io.FileType
import groovy.io.FileVisitResult
import groovy.transform.CompileStatic

class WorkingWithIOSpecTest extends GroovyTestCase {

    private final static boolean unixlike =
            System.getProperty('os.name').contains('Linux') ||
                    System.getProperty('os.name').contains('Mac OS')
   private final static boolean windoz =
            System.getProperty('os.name').contains('Windows')

    @CompileStatic
    private void doInTmpDir(Closure cl) {
        def baseDir = File.createTempDir()
        try {
            cl.call(new FileTreeBuilder(baseDir))
        } finally {
            baseDir.deleteDir()
        }
    }

    void testFileIntro() {
        doInTmpDir { dir ->
            File baseDir = dir.baseDir
            dir.'haiku.txt'('''Un vieil étang et
Une grenouille qui plonge,
Le bruit de l'eau.''')
            // tag::print_file_lines[]
            new File(baseDir, 'haiku.txt').eachLine { line ->
                println line
            }
            // end::print_file_lines[]

            // tag::print_file_lines2[]
            new File(baseDir, 'haiku.txt').eachLine { line, nb ->
                println "Line $nb: $line"
            }
            // end::print_file_lines2[]

            // tag::collect_lines[]
            def list = new File(baseDir, 'haiku.txt').collect {it}
            // end::collect_lines[]
            // tag::lines_as_strings[]
            def array = new File(baseDir, 'haiku.txt') as String[]
            // end::lines_as_strings[]
            assert list.size()==3
            assert array.length==3

            def file = new File(baseDir, 'haiku.txt')
            // tag::file_bytes[]
            byte[] contents = file.bytes
            // end::file_bytes[]
            def expectedLength = 64
            // e with acute accent won't be encoded on some systems -23 vs -61, -87
            if (System.getProperty('file.encoding').endsWith('1252')) {
                expectedLength = 63
            }
            assert contents.length == expectedLength

        }
    }

    void testWithReader() {
        doInTmpDir { dir ->
            File baseDir = dir.baseDir
            dir.'haiku.txt'('''Un vieil étang et
Une grenouille qui plonge,
Le bruit de l'eau.<commit_msg>remove checkout of byte length of file in testFileIntro, because it can fail depending on the system file encoding (e.g. fails on https://builds.apache.org ubuntu slaves)<commit_after>/*
 *  Licensed to the Apache Software Foundation (ASF) under one
 *  or more contributor license agreements.  See the NOTICE file
 *  distributed with this work for additional information
 *  regarding copyright ownership.  The ASF licenses this file
 *  to you under the Apache License, Version 2.0 (the
 *  "License"); you may not use this file except in compliance
 *  with the License.  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing,
 *  software distributed under the License is distributed on an
 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 *  KIND, either express or implied.  See the License for the
 *  specific language governing permissions and limitations
 *  under the License.
 */


package gdk

import groovy.io.FileType
import groovy.io.FileVisitResult
import groovy.transform.CompileStatic

class WorkingWithIOSpecTest extends GroovyTestCase {

    private final static boolean unixlike =
            System.getProperty('os.name').contains('Linux') ||
                    System.getProperty('os.name').contains('Mac OS')
   private final static boolean windoz =
            System.getProperty('os.name').contains('Windows')

    @CompileStatic
    private void doInTmpDir(Closure cl) {
        def baseDir = File.createTempDir()
        try {
            cl.call(new FileTreeBuilder(baseDir))
        } finally {
            baseDir.deleteDir()
        }
    }

    void testFileIntro() {
        doInTmpDir { dir ->
            File baseDir = dir.baseDir
            dir.'haiku.txt'('''Un vieil étang et
Une grenouille qui plonge,
Le bruit de l'eau.''')
            // tag::print_file_lines[]
            new File(baseDir, 'haiku.txt').eachLine { line ->
                println line
            }
            // end::print_file_lines[]

            // tag::print_file_lines2[]
            new File(baseDir, 'haiku.txt').eachLine { line, nb ->
                println "Line $nb: $line"
            }
            // end::print_file_lines2[]

            // tag::collect_lines[]
            def list = new File(baseDir, 'haiku.txt').collect {it}
            // end::collect_lines[]
            // tag::lines_as_strings[]
            def array = new File(baseDir, 'haiku.txt') as String[]
            // end::lines_as_strings[]
            assert list.size()==3
            assert array.length==3

            def file = new File(baseDir, 'haiku.txt')
            // tag::file_bytes[]
            byte[] contents = file.bytes
            // end::file_bytes[]
        }
    }

    void testWithReader() {
        doInTmpDir { dir ->
            File baseDir = dir.baseDir
            dir.'haiku.txt'('''Un vieil étang et
Une grenouille qui plonge,
Le bruit de l'eau.
{'fraction_non_alphanumeric': 0.09949354680607744, 'fraction_numerical': 0.004901159941186081, 'mean_word_length': 2.1540443070582174, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, 'https://': 1, 'lorem ipsum': 0, 'www.': 2, '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': '2550814', 'n_tokens_mistral': 1716, 'n_tokens_neox': 1665, 'n_words': 527}