<commit_before>// Copyright 2017 Google Inc.
//
// Licensed 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 base

import (
	"fmt"
	"os"
	"os/exec"
	"strings"
	"testing"
	"time"

	"github.com/stretchrcom/testify/assert"

	"github.com/soumya92/barista/bar"
	"github.com/soumya92/barista/base/scheduler"
	"github.com/soumya92/barista/outputs"
	testModule "github.com/soumya92/barista/testing/module"
)

// TestError tests that base.Error(error) behaves as expected: a nil error
// returns false and does nothing, a non-nil error returns true and updates
// the module's output.
func TestError(t *testing.T) {
	b := New()
	o := testModule.NewOutputTester(t, b)

	assert.True(t, b.Error(fmt.Errorf("test error")), "returns true for non-nil error")
	err := o.AssertError("on error")
	assert.Equal(t, "test error", err, "error message is displayed on the output")

	assert.False(t, b.Error(nil), "returns false for nil error")
	o.AssertNoOutput("on nil error")
}

// TestUpdateAndScheduler tests that update functions (including nil)
// are correctly handled, and that the returned scheduler works
// as intended.
func TestUpdateAndScheduler(t *testing.T) {
	scheduler.TestMode(true)
	b := New()
	o := testModule.NewOutputTester(t, b)

	assert.NotPanics(t, b.Update, "Calling update without setting OnUpdate")
	b.OnUpdate(nil)
	assert.NotPanics(t, b.Update, "Calling update with nil OnUpdate")

	updateCalled := false
	b.OnUpdate(func() {
		updateCalled = true
		b.Output(bar.Output{bar.NewSegment("test")})
	})

	assertUpdate := func(message string) bar.Output {
		out := o.AssertOutput(message)
		assert.True(t, updateCalled, message)
		updateCalled = false
		return out
	}

	assertNoUpdate := func(message string) {
		assert.False(t, updateCalled, message)
		o.AssertNoOutput(message)<commit_msg>Add tests for error clearing behaviour<commit_after>// Copyright 2017 Google Inc.
//
// Licensed 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 base

import (
	"fmt"
	"os"
	"os/exec"
	"strings"
	"testing"
	"time"

	"github.com/stretchrcom/testify/assert"

	"github.com/soumya92/barista/bar"
	"github.com/soumya92/barista/base/scheduler"
	"github.com/soumya92/barista/outputs"
	testModule "github.com/soumya92/barista/testing/module"
)

// TestError tests that base.Error(error) behaves as expected: a nil error
// returns false and does nothing, a non-nil error returns true and updates
// the module's output.
func TestError(t *testing.T) {
	b := New()
	o := testModule.NewOutputTester(t, b)

	assert.True(t, b.Error(fmt.Errorf("test error")), "returns true for non-nil error")
	err := o.AssertError("on error")
	assert.Equal(t, "test error", err, "error message is displayed on the output")

	assert.False(t, b.Error(nil), "returns false for nil error")
	o.AssertNoOutput("on nil error")

	b.Error(fmt.Errorf("something"))
	o.AssertError("on error")
	assert.NotNil(t, b.lastError, "last error is set")

	b.Output(outputs.Text("test"))
	o.AssertOutput("on regular output, in error state")
	assert.Nil(t, b.lastError, "last error is cleared on non-error output")

	b.Error(fmt.Errorf("something"))
	o.AssertError("on error")
	assert.NotNil(t, b.lastError, "last error is set")

	b.Clear()
	o.AssertOutput("on clear, in error state")
	assert.Nil(t, b.lastError, "last error is also cleared")
}

// TestUpdateAndScheduler tests that update functions (including nil)
// are correctly handled, and that the returned scheduler works
// as intended.
func TestUpdateAndScheduler(t *testing.T) {
	scheduler.TestMode(true)
	b := New()
	o := testModule.NewOutputTester(t, b)

	assert.NotPanics(t, b.Update, "Calling update without setting OnUpdate")
	b.OnUpdate(nil)
	assert.NotPanics(t, b.Update, "Calling update with nil OnUpdate")

	updateCalled := false
	b.OnUpdate(func() {
		updateCalled = true
		b.Output(bar.Output{bar.NewSegment("test")})
	})

	assertUpdate := func(message string) bar.Output {
		out := o.AssertOutput(message)
		assert.True(t, updateCalled, message)
		updateCalled = false
		return out
	}

	assertNoUpdate := func(message string) {
		assert.False(t, updateCalled, message)
		o.AssertNoOutput(message)
{'fraction_non_alphanumeric': 0.12465806955842126, 'fraction_numerical': 0.0062524423602969914, 'mean_word_length': 4.315680166147456, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, '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': '6390716', 'n_tokens_mistral': 1687, 'n_tokens_neox': 1639, 'n_words': 513}